使用HABTM在CakePHP中查找不显示引用表结果的查询

使用HABTM在CakePHP中查找不显示引用表结果的查询,php,mysql,sql,cakephp,has-and-belongs-to-many,Php,Mysql,Sql,Cakephp,Has And Belongs To Many,我有两个模型,代理和类别之间的HABTM关系。我遵循了这里的说明: 我已经设置了表、创建了FKs等,以便测试是否正在检索类别,以便在我放入以下调试代码的代理上查找查询: debug($this->AgentsCategories->find('all', array( 'order' => array('Agent.name', 'Agent.address'), 'conditions' => array('Agent.id' => '59')

我有两个模型,代理和类别之间的HABTM关系。我遵循了这里的说明:

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

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表中的信息,而不显示categories表中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')));

您将获得预期结果。

显示代理模型代码答案中应包含的两个单词
recursive
containable
=)。感谢您的帮助,我的代理模型看起来是这样的,查询看起来也是这样的,但输出与未显示类别数组的示例不匹配。Hi@AD7six,您的评论是什么意思?@dev808用于广泛控制find调用返回的内容。提供更精细的控制-它们是“答案”,没有它们,答案是不完整的。