2012-02-21 34 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です:

$hasAndBelongsToMany関係と
SELECT `Entity`.`ticker`, `Entity`.`id` FROM `entities` AS `Entity` WHERE `Country`.`id` = 78 LIMIT 1 

答えて

2

、あなたが自分自身をフィルタリングするモデルにfind()を呼び出すことがしばしば簡単です:

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

あなたが望む国とそれに関連するモデルのみが返されます。これは基本的にあなたが望むものです。

この場合、fieldsパラメータが関連するモデル(ここではエンティティ)で機能するかどうかは完全にはわかりませんが、そうでない場合は、代わりにCountryでContainableビヘイビアを使用できます。

+0

これは私をより近づけます。封筒は私が望むように働いていますが、私は新しい質問を開きます。 – opike

+1

「そうでない」と言いたい – opike

0

エンティティモデルクラスのhasAndBelongsToManyのの代わりの変化EntityGroup hasManyの協会タイプ

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

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