私はこれらの質問がたくさんあることを知っています。私はそれらのほとんどを使ってきましたが、どこが間違っているのか分からないので、私が行方不明になっているどこかの奇妙なタイプミス。ここに私のモデルは以下のとおりです。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
これは私をより近づけます。封筒は私が望むように働いていますが、私は新しい質問を開きます。 – opike
「そうでない」と言いたい – opike