gpt4 book ai didi

php - 使用 HABTM 查找未显示 CakePHP 中引用表结果的查询

转载 作者:行者123 更新时间:2023-11-30 00:09:06 25 4
gpt4 key购买 nike

我在两个模型(代理和类别)之间具有 HABTM 关系。我已按照此处的说明进行操作: http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html

我已经设置了表,创建了 FK 等。为了测试是否正在为代理上的查找查询检索类别,我放入了以下调试代码:

debug($this->AgentsCategories->find('all', array(
'order' => array('Agent.name', 'Agent.address'),
'conditions' => array('Agent.id' => '59')
)));

结果数组:

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'
)
)
)

仅显示代理表中的信息,不显示类别表中的类别(其中agents.id 为 FK)。根据说明,我期望类似下面的示例:

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
)
)
)

我做错了什么和/或我应该如何调试这个?谢谢!

最佳答案

您的代理模型应如下所示:

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' => ''
)
);
}

只需进行搜索即可:

$this->Agent->find('all', array('order' => array('Agent.name', 'Agent.address'), 
'conditions' => array('Agent.id' => '59')));

您将得到预期的结果。

关于php - 使用 HABTM 查找未显示 CakePHP 中引用表结果的查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24233947/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com