CakePHP:根据相关模型中的字段检索记录

发布于 2024-10-31 09:31:49 字数 2294 浏览 0 评论 0原文

我正在尝试根据相关模型中的字段进行搜索。我可以使用belongsTo模型来做到这一点,但不能使用hasMany模型。我确信这是我忽略的简单事情,但我看不到它。有人可以指出我正确的方向吗(或者甚至是我需要的蛋糕书页面 - 搜索过,但没有找到任何看起来像这样的东西)。谢谢

在组控制器中:(这不起作用)

$group = $this->Group->find('all', array('conditions' => array('Voucher.id' => '55')));

这确实:

$group = $this->Group->find('all', array('conditions' => array('TourOperator.id' => '3')));

后台文件片段:

组模型:

var $belongsTo = array(
        'TourOperator' => array(
            'className' => 'TourOperator',
            'foreignKey' => 'tour_operator_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

    var $hasMany = array(
        'Voucher' => array(
            'className' => 'Voucher',
            'foreignKey' => 'group_id',
            'dependent' => true,
            'conditions' => '',
            'fields' => '',
            'order' => 'Voucher.date, Voucher.meal_type_id'         
        )
    );

优惠券模型:

var $belongsTo = array(
        'Group' => array(
            'className' => 'Group',
            'foreignKey' => 'group_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

旅游运营商模型:

var $hasMany = array(
    'Group' => array(
        'className' => 'Group',
        'foreignKey' => 'tour_operator_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    )
);

更新(如下面的评论中发布的,但在这里阅读更清楚)

我最终使用了这个。

$groups = $this->Group->Voucher->find('all', array('fields' => 'DISTINCT group_id', 'conditions' => array('Voucher.status' => 'pending')));
$group_ids = Set::extract($groups, '/Voucher/group_id');
$data = $this->Group->find('all', array('conditions' => array('Group.id' => $group_ids)));

我获得了符合我的条件的组 ID 的不同列表,创建了一个仅包含 ID 的数组,然后使用它来拉取组,以便数组按照我在视图中的预期进行排序。

I'm trying to search based on a field in a related model. I can do so with the belongsTo model, but not the hasMany model. I'm sure this is something simple I'm overlooking but I can't see it. Could someone point me in the right direction (or even the cakebook page I need - searched, but didn't find anything that looked like this). Thanks

In groups controller: (this doesn't work)

$group = $this->Group->find('all', array('conditions' => array('Voucher.id' => '55')));

This does:

$group = $this->Group->find('all', array('conditions' => array('TourOperator.id' => '3')));

Background file snippets:

Group model:

var $belongsTo = array(
        'TourOperator' => array(
            'className' => 'TourOperator',
            'foreignKey' => 'tour_operator_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

    var $hasMany = array(
        'Voucher' => array(
            'className' => 'Voucher',
            'foreignKey' => 'group_id',
            'dependent' => true,
            'conditions' => '',
            'fields' => '',
            'order' => 'Voucher.date, Voucher.meal_type_id'         
        )
    );

Voucher model:

var $belongsTo = array(
        'Group' => array(
            'className' => 'Group',
            'foreignKey' => 'group_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

Tour Operator model:

var $hasMany = array(
    'Group' => array(
        'className' => 'Group',
        'foreignKey' => 'tour_operator_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    )
);

Update (as posted in comment below, but clearer to read here)

I've ended up using this.

$groups = $this->Group->Voucher->find('all', array('fields' => 'DISTINCT group_id', 'conditions' => array('Voucher.status' => 'pending')));
$group_ids = Set::extract($groups, '/Voucher/group_id');
$data = $this->Group->find('all', array('conditions' => array('Group.id' => $group_ids)));

I get a distinct list of group IDs matching my criteria, create an array of just the IDs and then use that to pull the groups so that the arrays are ordered as I expect them in my view.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

卖梦商人 2024-11-07 09:31:49

您能否将查询重新转换为 $this->Voucher->find,然后使用从该查询返回的关联组数据?正如您所说,通过 TourOperator id 查找团体没有问题,并且团体/优惠券关系看起来与此相反。

Can you recast your query to $this->Voucher->find, and then use the associated Group data returned from that query? As you say, there's no problem finding groups by their TourOperator id, and the Groups/Voucher relationship looks like it's the inverse of that.

迟到的我 2024-11-07 09:31:49

使用 CakePHP 的 Containable 行为,您无法在 hasMany 和 hasAndBelongsToMany 关联中的相关数据上使用条件。这是因为它使用单独的查询来获取这些关联。

但是,通过可链接行为,您可以使用 hasMany 关联中的数据来过滤记录。该行为可以在此处下载: https://github.com/Terr/linkable

Linkable 使用标准 JOIN 来,好,加入相关数据。这意味着它不会像普通的 SELECT 一样提取所有相关数据(即与您的组相关的所有优惠券)。然而,它可以与 Containable 一起工作来完成这两件事。

您可以在自述文件中找到 Linkable 如何工作的示例。

(免责声明:我目前维护 Linkable,但这不是我指出它的原因)

With CakePHP's Containable behaviour you can't use conditions on related data in hasMany and hasAndBelongsToMany associations. This is because it uses seperate queries to get those associations.

With the Linkable behaviour, however, you can use data from hasMany associations to filter records. The behaviour can be downloaded here: https://github.com/Terr/linkable

Linkable uses standard JOINs to, well, join related data. This means it doesn't pull in all the related data (ie, all the Vouchers related to your Group(s)), just like a normal SELECT. However, it can work together with Containable to do both.

You can find examples of how Linkable works in the README.

(disclaimer: I currently maintain Linkable, but that is not the reason I point to it)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文