cakephp来自控制器的深层条件

时间:2014-11-19 23:33:14

标签: cakephp

我正在尝试从我的BusinessesController中的CoursesEmployee模型中获取记录。它们没有直接联系,这是我的问题。

每个企业都有可以报名参加课程的员工。

BusinessesController.php:

    $options = array(
        'conditions' => array('Business.' . $this->Business->primaryKey => 1)
    );
    $test =  $this->Business->find('all', $options);
    debug($test);

输出:

array(
(int) 0 => array(
    'Business' => array(
        'id' => '1',
        'name' => 'Test Business',
    ),
    'Employee' => array(
        (int) 0 => array(
            'id' => '1',
            'business_id' => '1',
            'name' => 'Keith Power',
            'Business' => array(
                'id' => '1',
                'name' => 'Test Business',
            ),
            'Course' => array(
                (int) 0 => array(
                    'id' => '1',
                    'name' => 'Manual Handling',
                    'CoursesEmployee' => array(
                        'id' => '1',
                        'employee_id' => '1',
                        'course_id' => '1',
                        'course_module_id' => '1',
                        'completed' => true
                    )
                ),
                (int) 1 => array(
                    'id' => '3',
                    'name' => 'Induction',
                    'CoursesEmployee' => array(
                        'id' => '2',
                        'employee_id' => '1',
                        'course_id' => '3',
                        'course_module_id' => '0',
                        'completed' => false
                    )
                )
            )
        )
    )
)
)

我希望获得CoursesEmployee的列表,其中CoursesEmployee.completed = true且Business.id = 1

模型关联:

Business $ hasMany Employee

员工$ belongsTo Business

员工$ hasAndBelongsToMany课程

public $hasAndBelongsToMany = array(
    'Course' => array(
        'className' => 'Course',
        'joinTable' => 'courses_employees',
        'foreignKey' => 'employee_id',
        'associationForeignKey' => 'course_id',
        'unique' => 'keepExisting',
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
    )
);

CoursesEmployee

public $belongsTo = array(
    'Employee' => array(
        'className' => 'Employee',
        'foreignKey' => 'employee_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'Course' => array(
        'className' => 'Course',
        'foreignKey' => 'course_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
);

cakephp 2.5

1 个答案:

答案 0 :(得分:0)

我建议这样,如果它不起作用,我们将尝试加入方式:

已更新:

 $coursesEmployes = $this->Business->Employee->CoursesEmployee->find('all',array(
                                'conditions'=>array('CoursesEmployee.completed '=>  'true' ,'Employee.business_id' => '1' ),
                                'recursive'=>3)
                        );

希望它有所帮助!