我有三个表项目,任务和标签。 Projects.id 是第一个表的主键,Tasks.id 是第二个表的PK,Tags.id 是第三个表的PK。
$test = $this->Projects->find('all',
array(
'recursive' => 2
)
);
返回正确的数据。
但
$test = $this->Projects->find('all',
array(
'recursive' => 2,
'conditions' => array('Tags.id = ' => '10')
)
);
给出以下错误。 找不到列:1054 "where 子句"中的未知列"Tags.id"。
我确实有标签表的 id 字段,为什么会收到此错误?
项目模型代码段
public $primaryKey = 'id';
public $hasMany = array(
'Tasks' => array('className' => 'Tasks','foreignKey' => 'project_id')
);
任务模型代码片段
public $primaryKey = 'id';
public $hasMany = array(
'Tags' => array('className' => 'Tags','foreignKey' => 'task_id')
);
这是因为您无法传递许多关联模型字段的条件。
要完成这项工作,请在查找条件中传递"连接",这将正常工作。
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html