CakePHP中有许多模型
这是我的基本设置。。。家庭有很多学生有很多学生 如果注册为空,是否有办法排除学生?这是我的查找方法CakePHP中有许多模型,cakephp,join,containable,Cakephp,Join,Containable,这是我的基本设置。。。家庭有很多学生有很多学生 如果注册为空,是否有办法排除学生?这是我的查找方法 $options = array( 'order' => array('Family.family_last_name'), 'group' => 'Family.id', 'contain' => array( 'Student', 'Student.Enrollment' ),
$options = array(
'order' => array('Family.family_last_name'),
'group' => 'Family.id',
'contain' => array(
'Student', 'Student.Enrollment'
),
'joins' => array(
array(
'table' => 'students',
'alias' => 'Student',
'type' => 'left',
'conditions' => array(
'Family.id = Student.family_id'
)
),
array(
'table' => 'enrollment',
'alias' => 'Enrollment',
'type' => 'left',
'conditions' => array(
'Enrollment.student_id = Student.id'
)
)
),
'conditions' => array(
'Enrollment.status =' => 'withdrawn'
)
);
$enrollment = $this->Family->find( 'all', $options);
这是它返回的数组。我怎样才能拆下杰克
[Student] => Array
(
[0] => Array
(
[id] => 92
[first_name] => Jack
[Enrollment] => Array
(
)
)
[1] => Array
(
[id] => 93
[first_name] => Jill
[Enrollment] => Array
(
[0] => Array
(
[id] => 99
[student_id] => 93
[grade] => 4
)
)
)
)
当您加入注册时,请摆脱左侧的加入。将其改为内部连接
$options = array(
'order' => array('Family.family_last_name'),
'group' => 'Family.id',
'contain' => array(
'Student', 'Student.Enrollment'
),
'joins' => array(
array(
'table' => 'students',
'alias' => 'Student',
'type' => 'left',
'conditions' => array(
'Family.id = Student.family_id'
)
),
array(
'table' => 'enrollment',
'alias' => 'Enrollment',
'type' => 'inner',
'conditions' => array(
'Enrollment.student_id = Student.id'
)
)
),
'conditions' => array(
'Enrollment.status =' => 'withdrawn'
)
);
$enrollment = $this->Family->find( 'all', $options);
我不明白当你有可包含的模型时为什么要使用连接。顺便说一句,我发现了这个解决方案:它是有效的,但我不认为这是最好的方法 在您的
系列中
型号
public $hasMany = array
(
'Student' => array(
'conditions' => array('Student.id IN (SELECT DISTINCT student_id FROM enrollments)')
)
);
或者,如果愿意,您可以仅在需要时使用
Family->bindModel(array(
'hasMany' => array (
'Student' => array(
'conditions' => array('Student.id IN (SELECT DISTINCT student_id FROM enrollments)')
)
)
)
不。尝试了之后,它就忽略了注册。status=drawn不应该是
家庭
和学生
之间的连接,而应该是内部连接?
我实际上尝试了所有可能的连接组合LOL!有没有人在包含和连接一起工作时遇到过任何问题?谢谢Arilia,Containeable忽略了遥远的关联,我需要能够根据这些关联过滤结果。我会试试看它是否有效。