HABTM关系中的搜索记录 - CakePHP
我有一个商店和 store_details 表,现在有一个 store_details_stores 表。
Store 模型具有以下内容:
public $hasAndBelongsToMany = array('StoreDetail');
StoreDetail 模型具有以下内容:
public $hasAndBelongsToMany = array('Store');
当我在stores_controller 中尝试以下查询时,收到一个sql 错误。由于某种原因,store_details 表没有自然连接。这是预期的行为吗?我需要手动加入这个表吗?
$this->Store->find('all', array('conditions' => array('StoreDetail.name' => 'Parking')));
I have a stores and store_details table and now a store_details_stores table.
The Store model has the following:
public $hasAndBelongsToMany = array('StoreDetail');
The StoreDetail model has the following:
public $hasAndBelongsToMany = array('Store');
When I attempt the below query in the stores_controller, I receive an sql error. For some reason the store_details table is not being joined naturally. Is this expected behaviour? Do I need to join this table manually?
$this->Store->find('all', array('conditions' => array('StoreDetail.name' => 'Parking')));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是预期的行为。您需要根据您的条件设置一个 bindModel 调用,或者您可以为连接表创建一个模型并直接查询。
请参阅:
CakePHP 书籍 - HABTM
HABTM 连接表建模
This is expected behavior. You need to set up a bindModel call with your conditions, or you can create a model for your join table and query that directly.
See:
CakePHP Book - HABTM
Modelizing HABTM Join Tables
我建议您在查找之前使用 bindModel 方法将条件应用于关联。
I would recommend applying the condition to the association before you do your find, using the bindModel method.