Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用cakephp中的HABTM查找_Php_Cakephp_Cakephp 1.3 - Fatal编程技术网

使用cakephp中的HABTM查找

使用cakephp中的HABTM查找,php,cakephp,cakephp-1.3,Php,Cakephp,Cakephp 1.3,我知道有很多这样的问题,我已经讨论了其中的大部分问题,但由于我不知道我错在哪里,我想我可能是一个挑剔的打字错误,我错过了一些地方。以下是我的模型: class Country extends AppModel { var $name = 'Country'; var $hasAndBelongsToMany = array( 'Entity' => array( 'className' => 'Entity',

我知道有很多这样的问题,我已经讨论了其中的大部分问题,但由于我不知道我错在哪里,我想我可能是一个挑剔的打字错误,我错过了一些地方。以下是我的模型:

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错误(它不会尝试使用联接表构建查询):


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

使用
$HasAndBelongToMany
关系,通常更容易对要筛选自身的模型调用
find()

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

$blap = $this->Entity->find('all', array(
                                    'conditions' => array('CountriesEntities.country_id' => 78),
                                    'recursive' => 2
));
$this->Entity->Country->find('first', array('conditions' => array('Country.id' => 78)));
它将只返回您想要的国家及其相关模型,这基本上就是您想要的


在这种情况下,我不能完全确定
字段
参数在关联的模型(此处的实体)上是否有效,但如果不行,您可以在国家/地区使用可包含行为。

这让我更接近了。控制正在以我想要的方式工作,但我将提出一个新问题。
$this->Entity->Country->find('first', array('conditions' => array('Country.id' => 78)));