获取基于其他模特id Cake php的数据

时间:2014-02-24 10:18:37

标签: cakephp cakephp-2.0

ServiceController.php

public function admin_index() {
        $services=$this->Service->find('all', array('order'=>'Service.id desc'));
        $this->set('services', $services);
    }

admin_index.ctp (查看文件)

pr($services)
Array
(
    [Service] => Array
        (
            [id] => 53
            [user_id] => 65
            [first_name] => client
        )
    [User] => Array
            (
                [id] => 65
                [user_group_id] => 4
            )
    [SolutionType] => Array
            (
                [id] => 6
                [solution_type] => face to face interaction
                [status] => 0
            )

    [Service] => Array
        (
            [id] => 54
            [user_id] => 66
            [first_name] => client
        )
    [User] => Array
            (
                [id] => 66
                [user_group_id] => 5
            )
    [SolutionType] => Array
            (
                [id] => 6
                [solution_type] => face to face interaction
                [status] => 0
            )       
)

Service.php (模型文件)

public $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        ),
        'SolutionType' => array(
            'className' => 'SolutionType',
            'foreignKey' => 'solution_type_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
            )
        )

我想在ServicesController中显示条件显示User user_group_id = 5所有数据。

1 个答案:

答案 0 :(得分:5)

试试这个

 $services=$this->Service->find('all', array('conditions'=>array('User.user_group_id'=>5),
                                             'order'=> array('Service.id' => 'Desc')));