CakePHP可容纳条件不限制结果? [英] CakePHP containable conditions not limiting results?

查看:123
本文介绍了CakePHP可容纳条件不限制结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在分类列表中查找用户的杂货商品。关联是类别hasMany项目有多个用户通过杂货。我使用可控制行为,它不过滤掉所有其他杂货。

I'm trying to find a User's grocery items in a categorized list. The associations are Category hasMany Item hasMany User through Grocery. I'm using the Containable Behavior and it is not filtering out all other Grocery. It returns every item basically.

我的控制器函数:

function showlist() {
$categories = $this->Category->find('all', array(
    'contain' => array(
        'Item' => array(
            'Grocery' => array(
                'conditions' => array(
                    'Grocery.user_id =' => $this->Auth->user('id')
                )
            )
        )
    )
));

返回的数组:

Array
(
    [0] => Array
        (
            [Category] => Array
                (
                    [id] => 10
                    [parent_id] => 
                    [name] => Dairy
                    [lft] => 1
                    [rght] => 6
                )

            [Item] => Array
                (
                )

        )

[1] => Array
    (
        [Category] => Array
            (
                [id] => 11
                [parent_id] => 10
                [name] => Milk
                [lft] => 2
                [rght] => 3
            )

        **[Item] => Array
            (
            )**

    )

[2] => Array
    (
        [Category] => Array
            (
                [id] => 12
                [parent_id] => 10
                [name] => Cheese
                [lft] => 4
                [rght] => 5
            )

        [Item] => Array
            (
            )

    )

[3] => Array



我不想返回任何没有Grocery(ItemUser)

I don't want to return any Item arrays that don't have a Grocery(ItemUser) below them.

这样可以工作,但现在它只能平滑显示。我需要它来显示该嵌套数组中的一个类别的所有项目。

This works but now it only displays flatly. I need it to show all items of one category in that nested array.

它显示:

Array
(
[0] => Array
    (
        [Category] => Array **<--This could be the same category as**
            (...)

        [Item] => Array
            (...)

        [Grocery] => Array
            (...)
    )
[1] => Array
    (
        [Category] => Array **<--This**
            (...)

        [Item] => Array
            (...)

        [Grocery] => Array
            (...)
    )
[2]...

我需要:

Array
(
[0] => Array
    (
        [Category] => Array 
            (...
            [Item] => Array
            (...)

            [Item] => Array
            (...)
            )
    )
[1] => Array
    (
        [Category] => Array
            (...)

        [Item] => Array
            (...)
    )
[2]...


$ b

Don't even really need the Grocery array, just have to search thru them for criteria.

推荐答案

不能直接使用Grocery数组,根据Contain中的条件限制主模型的结果。当您使用 contains()时,它实际上会创建多个查询 - 这将阻止您根据包含的条件进行限制。

You cannot limit your main Model's results based on conditions within the Contain. When you use contain(), it actually creates multiple queries - that keeps you from being able to limit based on contained conditions.

要解决此问题,您需要使用JOINs [见CakePHP加入表] ,而不是包含。

To resolve this, you need to use JOINs [ see CakePHP Joining Tables ] instead of contain.

这篇关于CakePHP可容纳条件不限制结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆