[英]Find query not showing results from referenced table in CakePHP using HABTM
I have a HABTM relationship between two models, Agents and Categories. 我在两个模型(代理和类别)之间建立了HABTM关系。 I've followed the instructions here: http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html
我已按照此处的说明进行操作: http : //book.cakephp.org/2.0/en/models/associations-linking-models-together.html
I've setup the tables, created the FKs, etc. In order to test to see if Categories are being retrieved for a find query on Agents I put in the following debug code: 我已经设置了表,创建了FK,等等。为了测试是否正在针对Agent的查找查询检索类别,我将以下调试代码放入:
debug($this->AgentsCategories->find('all', array(
'order' => array('Agent.name', 'Agent.address'),
'conditions' => array('Agent.id' => '59')
)));
The resulting array: 结果数组:
array(
(int) 0 => array(
'Agent' => array(
'id' => '59',
'name' => 'MUTUAL UNDERWRITERS',
'address' => 'Waipahu Branch
94-615 Kupuohi St #102
Waipahu, HI 96797',
'telephone' => '808-688-2222',
'fax' => '808-688-0769',
'email' => null,
'website' => 'http://www.mutualunderwriters.com',
'island' => '1',
'modified' => '2014-04-16 15:56:46'
)
)
)
only shows info from the Agents table, it doesn't show categories from the categories table where agents.id is a FK. 仅显示来自“代理”表的信息,不显示来自类别表(其中agent.id是FK)的类别。 From the instructions I would expect something like the example below:
从说明中,我希望看到类似以下示例的内容:
Array
(
[Recipe] => Array
(
[id] => 2745
[name] => Chocolate Frosted Sugar Bombs
[created] => 2007-05-01 10:31:01
[user_id] => 2346
)
[Ingredient] => Array
(
[0] => Array
(
[id] => 123
[name] => Chocolate
)
[1] => Array
(
[id] => 124
[name] => Sugar
)
[2] => Array
(
[id] => 125
[name] => Bombs
)
)
)
What am I doing wrong and/or how should I debug this? 我在做什么错和/或应该如何调试? Thank you!
谢谢!
You Agent Model should looks like: 您的代理模型应如下所示:
class Agent extends AppModel {
public $hasAndBelongsToMany = array(
'Category' =>
array(
'className' => 'Category',
'joinTable' => 'agents_categories', //or your table name
'foreignKey' => 'agent_id',
'associationForeignKey' => 'category_id',
'unique' => true,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
'with' => ''
)
);
}
And simply do a search like: 只需进行如下搜索即可:
$this->Agent->find('all', array('order' => array('Agent.name', 'Agent.address'),
'conditions' => array('Agent.id' => '59')));
You will get the expected result. 您会得到预期的结果。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.