[英]CakePHP HABTM find … not returning expected results
I'm hoping somebody can clear up this issue I am having. 我希望有人能解决我遇到的这个问题。 None of the other answers on SO seemed to help me out for some reason. SO上的其他答案似乎都没有出于某种原因帮助我。
I have 2 tables with a HABTM relationship. 我有2个具有HABTM关系的表。 Publications have many authors, and authors have many publications. 出版物有很多作者,而作者有很多出版物。 In my case, I am attempting to output a list of all of the publications in the database, along with their corresponding authors. 就我而言,我试图输出数据库中所有出版物的列表,以及它们的相应作者。
I have the following tables: 我有以下表格:
0 TestPublication 0 TestPublication
0 John Doe A 0 John Doe A
0 0 0 0 0 0
The 'id' column of each table is set as the primary key. 每个表的“ id”列均设置为主键。
My Publication model looks like: 我的发布模型如下所示:
class Publication extends AppModel {
var $name = 'Publication';
var $hasAndBelongsToMany = array(
'Author'=>array(
'className'=>'Author'
)
);
}
And finally, the PublicationsController has the following function: 最后,PublicationsController具有以下功能:
function index() {
$publications = $this->Publication->find('all');
$this->set('publications', $publications);
}
Here is what publications now contains: 这是出版物现在包含的内容:
Array ( [0] => Array ( [Publication] => Array ( [id] => 0 [title] => TestPublica [type_id] => 1 ) [Author] => Array ( ) ) )
Why is this? 为什么是这样? I am expecting (perhaps that is my problem...) that the author John Doe should be present in the Author array. 我期望(也许是我的问题...)作者John Doe应该出现在Author数组中。 If it should be, where am I going wrong? 如果应该的话,我哪里出问题了? Do I need a bindModel call somewhere in there? 我是否需要在其中某处进行bindModel调用?
Or...is the code actually executing the way it should and my expectations are incorrect? 还是...代码实际上是按应有的方式执行并且我的期望不正确吗? If so, how would I return a list of all of the publications along with all of their authors? 如果是这样,我将如何返回所有出版物及其所有作者的清单?
Thanks for your time! 谢谢你的时间!
I believe I found the problem. 我相信我找到了问题。 I was manually inserting records into the DB for testing purposes. 我出于测试目的将记录手动插入数据库中。 I set the primary key id's on the first record of each table to zero. 我将每个表的第一条记录上的主键ID设置为零。 SQL DBs typically (or always?) start the first record with a primary key index of 1, not 0. I'm not sure WHY cake did not work with this, but changing the id's to 1 solved the problem. SQL DB通常(或总是?)以主键索引1(而不是0)开始第一条记录。我不确定为什么蛋糕不能与此配合使用,但是将id更改为1可以解决问题。 Weird... 奇怪的...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.