在cakephp中找到HABTM

时间:2012-02-21 04:13:50

标签: php cakephp cakephp-1.3

我知道有很多关于这些问题的问题而且我已经完成了大部分问题,但由于我无法弄清楚我哪里出错了,我想我可能会在某个地方找到一个挑剔的错字我失踪了。这是我的模特:

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

}

我想做一个基于国家ID返回所有实体的发现,即

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

这是我尝试过的find语句的最新版本:

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

这是Cake生成的sql生成sql错误(它不会尝试使用连接表构建查询):

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

3 个答案:

答案 0 :(得分:2)

$hasAndBelongsToMany关系,通常更容易在要过滤的模型上调用find()

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

它将仅返回您想要的国家及其相关模型,这基本上是您想要的。

在这种情况下,我不完全确定fields param在关联模型(此处为Entity)上有效,但如果没有,则可以使用Country中的Containable行为。

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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

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