CakePHP关于深层模型关联的条件 [英] CakePHP Conditions on deep model associations
问题描述
使用CakePHP 1.3,我有一个Find语句,其条件为第三级深关联:
酒店> Room> RoomType> name
像我的Find语句,只返回RoomTypes的名称为Suite的酒店。
我相信以下代码应该可以正常工作,错误:
$ this-> Hotel-> find('first',array(
' => array('Room'=> array('RoomType')),
'conditions'=> array(
'Hotel.Room.RoomType.name'=&
),
));
请注意,酒店将有许多客房,但每间客房只能有1 RoomType
我已经阅读过在contains语句中使用条件,但是只限制返回的RoomTypes,而不是整个酒店。
<我已经看到类似的问题张贴在这里,但我没有找到一个似乎解决这个问题。
将需要手动创建连接&条件作为查询的一部分:
$ this-> Hotel-> find('first',array $ b'contains'=> array(
'Room'=> array(
'RoomType'
)
),
'joins'=& array(
array(
'table'=>'rooms',
'alias'=>'Room',
'type'=>'inner'
'conditions'=> array(
'Room.hotel_id'=>'Hotel.id'
)
),
array 'table'=>'room_types',
'alias'=>'RoomType',
'type'=>'inner',
'conditions'=&
'RoomType.id'=>'Room.room_type_id',
'RoomType.name =>'Suit'
)
)
)$ b b);
Using CakePHP 1.3, I have a Find statement with a condition on the 3rd level deep association: Hotel > Room > RoomType > name
I would like my Find statement to only return Hotels with RoomTypes that have the Name "Suite".
I believe the following code should work, but it does not, stating an SQL syntax error:
$this->Hotel->find('first', array(
'contain' => array('Room' => array('RoomType')),
'conditions' => array(
'Hotel.Room.RoomType.name' => 'Suite'
),
));
Please note that a Hotel will have many Rooms, but each Room will only have 1 RoomType
I have read about using the condition within the contain statement, but that only limits the RoomTypes returned, not the overall Hotels.
I have seen similar questions posted here, but I haven't found one that seems to solve this issue.
You will need to manually create the joins & conditions as part of the query:
$this->Hotel->find('first', array(
'contain'=>array(
'Room'=>array(
'RoomType'
)
),
'joins'=>array(
array(
'table'=>'rooms',
'alias'=>'Room',
'type'=>'inner',
'conditions'=>array(
'Room.hotel_id'=>'Hotel.id'
)
),
array(
'table'=>'room_types',
'alias'=>'RoomType',
'type'=>'inner',
'conditions'=>array(
'RoomType.id'=>'Room.room_type_id',
'RoomType.name=>'Suit'
)
)
)
);
这篇关于CakePHP关于深层模型关联的条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!