繁体   English   中英

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

[英]Find query not showing results from referenced table in CakePHP using HABTM

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

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

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

仅显示来自“代理”表的信息,不显示来自类别表(其中agent.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')));

您会得到预期的结果。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM