简体   繁体   English

在Cakephp中使用HABTM查找

[英]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. 我知道在SO上有很多这样的问题,我已经解决了大多数问题,但是由于我无法弄清楚哪里出了问题,我认为我可能是我遗漏的挑剔错别字。 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 我想进行查找,以根据国家/地区ID返回所有实体,即

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: 这是我尝试过的find语句的最新迭代:

$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): 这是Cake生成的sql,它会生成sql错误(它不会尝试使用连接表来构建查询):

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: 通过$hasAndBelongsToMany关系,通常在要过滤自身的模型上调用find()更容易:

$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. 在这种情况下,我不能完全确定fields param是否可以在关联的模型(此处为Entity)上工作,但是如果不能,则可以在Country中使用Containable行为。

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

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

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

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

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