Cakephp用表连接分页

时间:2014-07-09 11:12:47

标签: php sql cakephp-2.0

在PeoplesController.php上,我想通过左连接添加Events表,有人可以指导我,因为我还在学习。这是原始和有效的代码。

public function constructClasses() {
    parent::constructClasses();
    $this->loadModel('People');
    $this->loadModel('Event');
}

public function view() {
    $num_per_page = 8;
    if($tab == "images") {
        $conditions = array();

        $this->paginate['Tagged'] = array(
            'tagged',
            'model' => 'Image',                        
            'by' => $people['People']['slug'],
            'conditions' => $conditions,                                         
            'order' => array('Image.created'=>'DESC'),
            'limit' => $num_per_page,
            'paramType' => 'querystring',
        );
        $related = $this->paginate('Tagged');                     
    }
}

这是输出:

            array(
                (int) 0 => array(
                    'Image' => array(
                        'object_id' => '1098',
                    )
                ),
                ....
                ....
                (int) 7 => array(
                    'Image' => array(
                        'object_id' => '1082',
                    )
                )
            )

目标是通过左连接从事件表中获取slug Event.id = Image.object_id

            Events table
            -----------------------
            id      | slug
            -----------------------
            1098    | slug-of-1098
            1082    | slug-of-1082

目标输出:

            array(
                (int) 0 => array(
                    'Image' => array(
                        'object_id' => '1098',
                        'slug' => 'slug-of-1098'
                    )
                ),
                ....
                ....
                (int) 7 => array(
                    'Image' => array(
                        'object_id' => '1082',
                        'slug' => 'slug-of-1082'
                    )
                )
            )                

0 个答案:

没有答案