简体   繁体   中英

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

I have a HABTM relationship between two models, Agents and Categories. I've followed the instructions here: 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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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