0

我正在尝试在分类列表中查找用户的杂货。关联是 Category hasMany Item hasMany User through Grocery。我正在使用 find() 的 Joins 选项并在我的数组中获取正确的信息,但是我需要它以不同的方式显示。

我的控制器动作:

 function showlist() {
    $this->Category->recursive = -1;
    $options['joins'] = array(
        array('table' => 'items',
            'alias' => 'Item',
            'type' => 'left',
            'foreignKey' => false,
            'conditions' => array(
                'Item.category_id = Category.id'
            )
        ),
        array('table' => 'items_users',
            'alias' => 'Grocery',
            'type' => 'left',
            'foreignKey' => false,
            'conditions' => array(
                'Grocery.item_id = Item.id'
            )
        )
    );
    $options['conditions'] = array(
        'Grocery.user_id' => $this->Auth->user('id')
    );
    $options['fields'] = array(
        'Category.*', 'Item.*', 'Grocery.*'
    );

    $categories = $this->Category->find('all', $options);

    $this->set('categories', $categories);
}

这有效,但现在它只显示平坦。我需要它在嵌套数组中显示一个类别的所有项目。

它显示:

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]...

甚至不需要 Grocery 数组,只需通过它们搜索条件即可。


新代码

控制器:

function showlist() {
    $this->Category->recursive = -1;
    $options['joins'] = array(
        array('table' => 'items',
            'alias' => 'Item',
            'type' => 'left',
            'foreignKey' => false,
            'conditions' => array(
                'Item.category_id = Category.id'
            )
        ),
        array('table' => 'items_users',
            'alias' => 'Grocery',
            'type' => 'left',
            'foreignKey' => false,
            'conditions' => array(
                'Grocery.item_id = Item.id'
            )
        )
    );
    $options['conditions'] = array(
        'Grocery.user_id' => $this->Auth->user('id')
    );
    $options['fields'] = array(
        'Category.*', 'Item.*'
    );
    $options['contain'] = array(
            'Item' 
        );

    $categories = $this->Category->find('all', $options);

    $this->set('categories', $categories);
}

结果数组:

Array
(
    [0] => Array
    (
        [Category] => Array
            (
                [id] => 31
                [parent_id] => 
                [name] => Cereal
                [lft] => 21
                [rght] => 22
            )

        [Item] => Array
            (
                [id] => 23
                [name] => Granola Bars
                [category_id] => 31
                [description] => 
                [user_id] => 
                [0] => Array
                    (
                        [id] => 23
                        [name] => Granola Bars
                        [category_id] => 31
                        [description] => 
                        [user_id] => 
                    )

                [1] => Array
                    (
                        [id] => 44
                        [name] => Fiber Bars
                        [category_id] => 31
                        [description] => 
                        [user_id] => 
                    )

            )

    )

[1] => Array
    (
        [Category] => Array
            (
                [id] => 15
                [parent_id] => 27
                [name] => Household
                [lft] => 31
                [rght] => 32
            )

        [Item] => Array
            (
                [id] => 24
                [name] => Cleaning Cloths
                [category_id] => 15
                [description] => 
                [user_id] => 
                [0] => Array
                    (
                        [id] => 24
                        [name] => Cleaning Cloths
                        [category_id] => 15
                        [description] => 
                        [user_id] => 
                    )

                [1] => Array
                    (
                        [id] => 25
                        [name] => Air Freshener
                        [category_id] => 15
                        [description] => 
                        [user_id] => 
                    )

                [2] => Array
                    (
                        [id] => 28
                        [name] => Paper Towels
                        [category_id] => 15
                        [description] => 
                        [user_id] => 
                    )

            )

    )

[2] => Array
    (
        [Category] => Array
            (
                [id] => 15
                [parent_id] => 27
                [name] => Household
                [lft] => 31
                [rght] => 32
            )

        [Item] => Array
            (
                [id] => 25
                [name] => Air Freshener
                [category_id] => 15
                [description] => 
                [user_id] => 
                [0] => Array
                    (
                        [id] => 24
                        [name] => Cleaning Cloths
                        [category_id] => 15
                        [description] => 
                        [user_id] => 
                    )

                [1] => Array
                    (
                        [id] => 25
                        [name] => Air Freshener
                        [category_id] => 15
                        [description] => 
                        [user_id] => 
                    )

                [2] => Array
                    (
                        [id] => 28
                        [name] => Paper Towels
                        [category_id] => 15
                        [description] => 
                        [user_id] => 
                    )

            )

    )

[3] => Array

请注意它是如何在直接在类别下的项目下再次包含项目的。直接在类别下显示的项目是我想要的,它包含在杂货列表中。find() 现在返回我正在寻找的项目下的数组中顶级类别中的所有项目。

另一个问题是 cake 没有对同一类别的项目进行分组。因此,如果空气清新剂和纸巾在同一个杂货清单上,它们就不会出现在同一个类别中,该类别将被列出两次。

4

1 回答 1

0

编辑

我刚刚阅读了您的另一个问题,最好将其包含在这篇文章中。所以你想将 Categories 的结果限制为找到的 Grocery 模型的结果?

我建议您尝试从最低的模型开始(我在我的应用程序中也这样做了):

$categories = $this->Grocery->Item->Category->find('all', $options);

你为什么不使用 Containable?这似乎是使用它的完美情况。

将以下内容添加到您的模型中:

public $actsAs = array('Containable');

然后像这样检索您的数据:

$this->Category->find('all', array('contain' => array('Item' => array('contain' =>    array('Grocery'=> array('conditions' => array('Grocery.user_id' => $this->Auth->user('id'))))))));
于 2013-03-31T19:50:17.117 回答