2012-02-21 60 views
0

我知道這裏有很多這樣的問題,我已經通過了其中的大部分問題,但是由於我無法弄清楚我要去哪裏出錯,我想我可能是一個硝煙繚繞的人,挑剔的錯字我失蹤的地方。這裏是我的模型:用cakephp在HABTM中查找

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 

這裏的發現聲明我試過的最新迭代:

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

這是蛋糕生成生成SQL錯誤(它不會嘗試建立與連接表的查詢)的SQL:

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

回答

2

$hasAndBelongsToMany關係,往往更容易呼籲要篩選自己的模型find()

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

它只會返回你想要的國家及其相關模型,這基本上就是你想要的。

在這種情況下,我並不完全確定fields參數對相關模型(此處的實體)有效,但如果不是,則可以使用Country中的Containable行爲。

+0

這讓我更接近。包含按我想要的方式工作,但我會打開一個新問題。 – opike 2012-02-21 14:02:20

+1

想說「不是」 – opike 2012-03-08 14:41:22

0

變化EntityGroup的hasMany到位hasAndBelongsToMany協會類型的實體模型類

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

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