简体   繁体   中英

Find with HABTM in cakephp

I know there are a bunch of these questions on SO and I've been through most of them but since I can't figure out where I'm going wrong I think I might be a nit-picky typo somewhere that I'm missing. Here are my models:

class Country extends AppModel {
    var $name = 'Country';

    var $hasAndBelongsToMany = array(
        'Entity' => array(
            'className' => 'Entity',
            'joinTable' => 'countries_entities',
            'foreignKey' => 'country_id',
            'associationForeignKey' => 'entity_id',
            'unique' => true,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
            'deleteQuery' => '',
            'insertQuery' => ''
        )
    );

}

class Entity extends AppModel {
    var $name = 'Entity';

    var $hasMany = array(
        'Alert' => array(
            'className' => 'Alert',
            'foreignKey' => 'entity_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );


    var $hasAndBelongsToMany = array(
        'EntityGroup' => array(
            'className' => 'EntityGroup',
            'joinTable' => 'entities_entity_groups',
            'foreignKey' => 'entity_id',
            'associationForeignKey' => 'entity_group_id',
            'unique' => true,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
            'deleteQuery' => '',
            'insertQuery' => ''
        ),
        'Country' => array(
            'className' => 'Country',
            'joinTable' => 'countries_entities',
            'foreignKey' => 'entity_id',
            'associationForeignKey' => 'country_id',
            'unique' => true,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
            'deleteQuery' => '',
            'insertQuery' => ''
        )
    );

}

I want to do a find that returns all of the entities based off of a country id, ie

select entities.ticker from entities
join countries_entities on entities.id=countries_entities.entity_id
where country_id=78

Here's the latest iteration of the find statement I tried:

$blap = $this->Entity->find('all', array('recursive'=>1,'fields'=> 'Entity.ticker','conditions'=>array('Country.id'=>78)));

And this is the sql that Cake generates which generates a sql error (it doesn't try to build the query with the join table):

SELECT `Entity`.`ticker`, `Entity`.`id` FROM `entities` AS `Entity` WHERE `Country`.`id` = 78 LIMIT 1

with $hasAndBelongsToMany relations, it is often easier to call the find() on the model you want to filter itself:

$this->Entity->Country->find('first', array('conditions' => array('Country.id' => 78)));

It will return only the Country you want, and its associated models, which is basically what you want.

In this case, I'm not completely sure the fields param works on the associated model (Entity here), but if it doesn't, you can use the Containable behavior in Country instead.

在实体模型类中更改EntityGroup hasMany的关联类型代替hasAndBelongsToMany

$this->Entity->bindModel(array('hasOne' => array('CountriesEntities')));

$blap = $this->Entity->find('all', array(
                                    'conditions' => array('CountriesEntities.country_id' => 78),
                                    'recursive' => 2
));

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