开发者

How to use one controller for more than one sql query in Cakephp?

I have a serious deadlock about my project. I am looking for a solution for days but there is nothing.

My index page have 4 different mysql queries. I tried to code this at first inside 1 controller and 1 view. It was not the right way. Then somebody suggested using 4 elements with requestAction() function. It was very good at first. Then I needed mysql between command. I could not found a way for it to use with requestAction(). My question on Cakephp group still unanswered. Somebody suggested using actions in controller. But I couldn't figure it out how to create that controller.

Please tell me what you know about it. Thanks in advance,

Here is my current post controller's index function:

function index() {
        $posts = $this->paginate();
        if (isset($this->params['requeste开发者_运维知识库d'])) {
            return $posts;
        } else {
            $sql = array( 'conditions' => array( 'id BETWEEN ? AND ?' => array( 286, 291 ) ) );
            $this->Post->find('all', $sql);
            $this->set('posts', $posts);
        }
}

What should I do? Should I add a new controller for rest 3 actions? Or should I do something inside index() function?


Every time I need several queries in the single controller I do this way:

// 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')); 
}

I guess, you want to paginate on those 3 columns so the example above is not good for that.

I think you have an error in your method. $posts is not related to your find. There should be $posts = $this->Post->find('all', $sql);. But this would not allow you to paginate on the result. Take a look at Custom Query Pagination in the manual. Maybe this would work for you:

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());
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜