CakePHP 深度模型关联的条件
使用 CakePHP 1.3,我有一个 Find 语句,其条件位于第三级深度关联上: 酒店>房间>房间类型> name
我希望我的 Find 语句仅返回 RoomTypes 名称为“Suite”的酒店。
我相信下面的代码应该可以工作,但它不能工作,说明 SQL 语法错误:
$this->Hotel->find('first', array(
'contain' => array('Room' => array('RoomType')),
'conditions' => array(
'Hotel.Room.RoomType.name' => 'Suite'
),
));
请注意,一家酒店将有许多房间,但每个房间只会有 1 个 RoomType
我已阅读有关在包含语句中使用条件的信息,但是这只限制返回的 RoomTypes,而不限制整个酒店。
我在这里看到过类似的问题,但我还没有找到似乎可以解决此问题的问题。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将需要手动创建连接和连接。条件作为查询的一部分:
You will need to manually create the joins & conditions as part of the query: