HABTM关系中的搜索记录 - CakePHP

发布于 2024-12-19 18:26:41 字数 494 浏览 2 评论 0原文

我有一个商店和 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

另类 2024-12-26 18:26:41

这是预期的行为。您需要根据您的条件设置一个 bindModel 调用,或者您可以为连接表创建一个模型并直接查询。

请参阅:

CakePHP 书籍 - HABTM

HABTM 连接表建模

$this->Store->bindModel(array(
    'hasAndBelongsToMany' => array(
            'StoreDetail' => array('conditions'=>array('StoreDetail.name' => 'Parking')
))));
$this->Store->find('all');

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

$this->Store->bindModel(array(
    'hasAndBelongsToMany' => array(
            'StoreDetail' => array('conditions'=>array('StoreDetail.name' => 'Parking')
))));
$this->Store->find('all');
白云不回头 2024-12-26 18:26:41

我建议您在查找之前使用 bindModel 方法将条件应用于关联。

/**
 * Apply a condition to the association
 */
$this->Store->bindModel(array(
    'hasAndBelongsToMany' => array(
        'StoreDetail' => array('conditions' => array('StoreDetail.name'=>'Parking'))
)));

/**
 * Find all Stores that have an associated StoreDetail.name of 'Parking'
 */
$this->Store->find('all');

I would recommend applying the condition to the association before you do your find, using the bindModel method.

/**
 * Apply a condition to the association
 */
$this->Store->bindModel(array(
    'hasAndBelongsToMany' => array(
        'StoreDetail' => array('conditions' => array('StoreDetail.name'=>'Parking'))
)));

/**
 * Find all Stores that have an associated StoreDetail.name of 'Parking'
 */
$this->Store->find('all');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文