我知道這裏有很多這樣的問題,我已經通過了其中的大部分問題,但是由於我無法弄清楚我要去哪裏出錯,我想我可能是一個硝煙繚繞的人,挑剔的錯字我失蹤的地方。這裏是我的模型:用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
這讓我更接近。包含按我想要的方式工作,但我會打開一個新問題。 – opike 2012-02-21 14:02:20
想說「不是」 – opike 2012-03-08 14:41:22