我有三张桌子: 商店 deliverable_sets 城市
以下关系:
//Store
public $hasMany = array(
'DeliverableSet' => array(
'className' => 'DeliverableSet',
'foreignKey' => 'store_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
//DeliverableSet
public $belongsTo = array(
'Store' => array(
'className' => 'Store',
'foreignKey' => 'store_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
//DeliverableSet
public $hasAndBelongsToMany = array(
'City' => array(
'className' => 'City',
'joinTable' => 'cities_deliverable_sets',
'foreignKey' => 'deliverable_set_id',
'associationForeignKey' => 'city_id',
'unique' => 'keepExisting',
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'finderQuery' => '',
)
);
现在,我想通过deliveradble_set获取分配给商店的所有城市的列表。
我已经尝试过这段代码,但它不起作用。
$data = $this->DeliverableSet->Store->find('all', array(
'joins' => array(
array(
'table' => 'deliverable_sets',
'alias' => 'DeliverableSetJoin',
'type' => 'INNER',
'conditions' => array(
'DeliverableSetJoin.store_id = Store.id'
)
),
array(
'table' => 'cities_deliverable_sets',
'alias' => 'CityDeliverableSetJoin',
'type' => 'INNER',
'conditions' => array(
'CityDeliverableSetJoin.deliverable_set_id = DeliverableSetJoin.id'
),
array(
'table' => 'cities',
'alias' => 'CityJoin',
'type' => 'INNER',
'conditions' => array(
'CityDeliverableSetJoin.city_id = CityJoin.id'
)
)
),
'conditions' => array(
'Store.id' => 11
),
'fields' => array('DeliverableSetJoin.id', 'Store.id','CityJoin.id'),
'order' => 'Store.created DESC'
)));
debug($data);exit;
基本上我想在cakephp的find方法中运行这个sql:
$set = $this->DeliverableSet->query('select cities_deliverable_sets.city_id from stores
inner join deliverable_sets on deliverable_sets.store_id = stores.id
inner join cities_deliverable_sets on cities_deliverable_sets.deliverable_set_id = deliverable_sets.id
WHERE stores.id = 11
order by stores.id asc');
答案 0 :(得分:1)
现在可行:
$this->DeliverableSet->Store = -1;
$data = $this->DeliverableSet->Store->find('all', array(
'joins' => array(
array(
'table' => 'deliverable_sets',
'alias' => 'DeliverableSetJoin',
'type' => 'INNER',
'conditions' =>'DeliverableSetJoin.store_id = Store.id'
),
array(
'table' => 'cities_deliverable_sets',
'alias' => 'CityDeliverableSetJoin',
'type' => 'INNER',
'conditions' =>'CityDeliverableSetJoin.deliverable_set_id = DeliverableSetJoin.id'
),
array(
'table' => 'cities',
'alias' => 'CityJoin',
'type' => 'INNER',
'conditions' =>'CityDeliverableSetJoin.city_id = CityJoin.id'
)
),
'conditions' => array(
'Store.id' => 18
),
'fields' => array('DeliverableSetJoin.id', 'Store.id','CityJoin.id'),
'order' => 'Store.created DESC'
));
以下sql的结果:
'SELECT `DeliverableSetJoin`.`id`, `Store`.`id`, `CityJoin`.`id` FROM `directorylisting`.`stores` AS `Store`
INNER JOIN `directorylisting`.`deliverable_sets` AS `DeliverableSetJoin` ON (`DeliverableSetJoin`.`store_id` = `Store`.`id`)
INNER JOIN `directorylisting`.`cities_deliverable_sets` AS `CityDeliverableSetJoin` ON (`CityDeliverableSetJoin`.`deliverable_set_id` = `DeliverableSetJoin`.`id`) INNER JOIN `directorylisting`.`cities` AS `CityJoin` ON (`CityDeliverableSetJoin`.`city_id` = `CityJoin`.`id`) WHERE `Store`.`id` = 18 ORDER BY `Store`.`created` DESC'