我一直在寻找可以容纳CakePHP的很长一段时间,不知怎的,我得到的印象是CakePHP代码中有一个错误...让我给你以下例子
假设与“指令”相关的模型“会计”。
public $belongsTo = array('Instruction');
我像这样加入:
$options['contain'] = array(
'Instruction' => array(
'fields' => array('Instruction.id', 'Instruction.main_student_id', 'Instruction.teacher_id'),
'Student' => array(
'fields' => array('Student.id', 'Student.full_name', 'Student.invoice_payer_id'),
),
'Teacher' => array(
'fields' => array('Teacher.id', 'Teacher.full_name'),
'conditions' => array('Teacher.id' => $teacher_id)
),
)
);
return parent::find('all', $options);
所以有一个指导和一个学生和一个老师属于这个指令。我从“会计”模型中调用了find
示例:
但让我们更进一步:
请你帮我理解...... - 为什么连接错误地完成? - 为什么不检索所有字段?
答案 0 :(得分:1)
Containable also goes a step deeper: you can filter the data of the associated models.
您对可包含项的条件用作过滤器,而不是对连接的限制。因此,它不会返回某位教师的说明,而是返回所有说明及其相关教师,以条件array('Teacher.id' => $teacher_id)
尝试在教学中添加条件而不是教师。类似的东西:
'Instruction' => array(
'Student','Teacher',
'conditions' => array('Instruction.teacher_id' => $teacher_id)
这将返回所有会计,它只会过滤某位老师的指示。
你总是可以手动进行连接(它看起来不太好但是有效),如果你这样做,只记得将containsable设置为false。
希望这有帮助