我定义了以下模型关系:
class Publication extends AppModel {
var $name = 'Publication';
var $hasAndBelongsToMany = array(
'Author'=>array(
'className'=>'Author'
)
);
}
class Author extends AppModel {
var $name = 'Author';
var $hasAndBelongsToMany = array(
'Publication'=>array(
'className'=>'Publication'
)
);
var $belongsTo = array(
'College' => array (
'className' => 'College'
)
);
}
class College extends AppModel {
var $name = 'College';
var $hasMany = array(
'Department'=>array(
'className'=>'Department'
)
);
}
class Department extends AppModel {
var $name = 'Department';
var $belongsTo = array(
'College'=>array(
'className'=>'College'
)
);
}
数据库表设置正确(HABTM 的连接表等)。我试图找到一个数据库查询来统治它们。我想创建一个查询,以查找所有相关作者、学院、部门等的出版物。从表单中获取数据后,我尝试运行如下查询:
$conditions = array(
"Author.id" => $this->data['authors'],
"Publication.year" => $this->data['year']
);
$publications = $this->Publication->find('all', array('conditions' => $conditions));
这会引发 SQL 错误,指出 Author.id 不是有效字段。现在,这是因为在搜索 Author.id 时尚未完成与“authors”数据库表的连接。但是,如果我这样做:
$pubs = $this->Publication->find('all', array('conditions' => $conditions));
然后我得到一个数组,其中包含所有相关作者的所有出版物(但由于某种原因,不是相关的大学)。
我的问题是:在搜索 Author.id 之前,我需要做什么才能使表加入?我尝试使用 bindModel、containable、子查询,但由于某种原因(可能是 ID10T 错误)无法使它们工作。
感谢您的任何建议!
编辑:以下调用的结果:
$this->Publication->recursive = 2;
$pubs = $this->Publication->find('all');
如下面所述:
Array (
[0] => Array ( [Publication] => Array ( [id] => 1 [title] => TestArticle [year] => 2011 [type_id] => 3 ) [Type] => Array ( [id] = > 3 [type_name] => Journal Articles ) [Author] => Array ( [0] => Array ( [id] => 2 [firstname] => Jeremy [lastname] => Gustine [middle] => A [faculty ] => 0 [college_id] => 4 [AuthorsPublication] => 数组 ( [id] => 3 [author_id] => 2 [publication_id] => 1 ) [College] => 数组 ( [id] => 4 [ College_name] => 文学、艺术和科学 ) ) [1] => 数组 ( [id] => 3 [firstname] => George [lastname] => Obama [middle] => A [faculty] => 0 [ College_id] => 6 [AuthorsPublication] => 数组([id] => 2 [author_id] => 3 [publication_id] =>1 ) [College] => Array ( [id] => 6 [college_name] => 公共事务学院) ) [2] => Array ( [id] => 2 [firstname] => Jeremy [lastname] => Gustine [中间] => A [faculty] => 0 [college_id] => 4 [AuthorsPublication] => Array ([id] => 1 [author_id] => 2 [publication_id] => 1) [College] => Array ( [id] => 4 [college_name] => Letters, Arts, and Sciences) ) ) ) [1] => Array ( [Publication] => Array ( [id] => 2 [title] => TestBook [ year] => 2010 [type_id] => 1 ) [Type] => Array ( [id] => 1 [type_name] => Books ) [Author] => Array ( [0] => Array ( [id] = > 7 [firstname] => Sony [lastname] => Stuff [middle] => L [faculty] => 0 [college_id] =>5 [AuthorsPublication] => 数组([id] => 4 [author_id] => 7 [publication_id] => 2)[College] => 数组([id] => 5 [college_name] => 护理与健康科学) ) ) ) )
希望这有点可读...