我有以下的模型關係定義:CakePHP的 - 一個發現()查詢統治他們
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'db表的連接。但是,如果我這樣做:
$pubs = $this->Publication->find('all', array('conditions' => $conditions));
然後我得到具有所有與所有相關作者的出版物的數組(雖然,不包括相關的院校出於某種原因)。
我的問題是這樣的:我需要做什麼才能使表格在加入Author.id之前加入?我試圖使用bindModel,可容納的子查詢,但無法讓這些工作出於某種原因(可能是一個ID10T錯誤)。
感謝您的任何建議!
編輯: 以下調用的結果:
$this->Publication->recursive = 2;
$pubs = $this->Publication->find('all');
如下:
Array (
[0] =>數組( [文獻] =>數組([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] => Array([id] => 3 [author_id] => 2 [publication_id] => 1 ) [College] => Array([id] => 4 [college_name] => Letters,Arts,and Sciences)) [1] => Array([id] => 3 [firstname] => George [lastname ] =>奧巴馬[中] => A [faculty] => 0 [college_id] => 6 [AuthorsPublication] => Array([id] => 2 [author_id] => 3 [publication_id] => 1) [學院] =>陣列([id] => 6 [college_name] =>公共事務學院)) [2] =>陣列([id] => 2 [firstname] => Jeremy [lastname] =>中等] => A [faculty] => 0 [college_id] => 4 [AuthorsPublication] =>數組(=)[數組] ([id] => 4 [college_name] =>文字,藝術和科學)))) [1] => Array( [Publication] => Array([id] => 2 [title] => TestBook [year] => 2010 [type_id] => 1) [Type] => Array([ [] => 1 [type_name] => Books) [Author] => Array( [0] => Array([id] => 7 [firstname] => Sony [lastname] => Stuff [middle] = > L [faculty] => 0 [college_id] => 5 [AuthorsPublication] => Array([id] => 4 [author_id] => 7 [publication_id] => 2) [College] => ] => 5 [college_name] =>護理和健康科學)))))
希望這是有點可讀...
大學學院不應該有很多作者嗎? – 2012-07-06 09:03:44
現在你提到它,是的,我認爲它應該。雖然即使沒有在大學模式中定義的這種關聯,我現在也可以通過出版物找到相關的學院,我現在正在使用Rikesh的建議。 – 2012-07-06 13:14:57