1

我的项目陷入了严重的僵局。我几天来一直在寻找解决方案,但什么也没有。

我的索引页面有 4 个不同的 mysql 查询。我首先尝试在 1 个控制器和 1 个视图中对此进行编码。这不是正确的方法。然后有人建议使用带有 requestAction() 函数的 4 个元素。一开始非常好。然后我在命令之间需要mysql。我找不到与 requestAction() 一起使用的方法。我对 Cakephp 小组的问题仍未得到解答。有人建议在控制器中使用动作。但我无法弄清楚如何创建该控制器。

请告诉我你对它的了解。提前致谢,

这是我当前的后控制器的索引函数:

function index() {
        $posts = $this->paginate();
        if (isset($this->params['requested'])) {
            return $posts;
        } else {
            $sql = array( 'conditions' => array( 'id BETWEEN ? AND ?' => array( 286, 291 ) ) );
            $this->Post->find('all', $sql);
            $this->set('posts', $posts);
        }
}

我该怎么办?我应该为其余 3 个动作添加一个新控制器吗?或者我应该在 index() 函数中做些什么?

4

1 回答 1

1

每次我需要在单个控制器中进行多个查询时,我都会这样做:

// model Foo
function edit($id){
    $this->data = $this->Foo->read(null, $id);
    $second = $this->Foo->Foo2->find('list');
    $third = $this->Foo->Foo3->find('list');
    $this->set(compact('second', 'third')); 
}

我想,你想在这 3 列上分页,所以上面的例子不适合。

我认为您的方法有误。$posts与你的无关find。应该有$posts = $this->Post->find('all', $sql);。但这不允许您对结果进行分页。查看手册中的自定义查询分页。也许这对你有用:

function index(){
    if(!isset($this->params['requested'])) {
        $this->paginate = array('Post' => array(
            'conditions' => array('id BETWEEN ? AND ?' => array(286, 291))
        ));
    }
    $this->set('posts', $this->paginate());
}
于 2010-07-07T12:23:46.553 回答