Cakephp:包含行为,数组数组的条件

时间:2015-03-12 13:21:28

标签: cakephp conditional-statements contain

我遇到包含行为条件和空数组的问题。

部分代码:

   $menusTopUser = $this->Menu->find('all', array(
            'contain' => array('TemplateRegion', 'Language', 'Item', 'MenusUserGroup' => array('conditions' => array('MenusUserGroup.user_group_id ' => $this->Auth->user('UserGroup.id')))
            ),
            'conditions' => array(
                'TemplateRegion.alias' => 'menu_top_user',
                'Menu.active' => 1,
                'Language.ISO-639-2' => $this->Session->read('Config.frontendLanguage'),
            ),
            'fields' => array('Menu.title', 'Item.slug', 'Menu.url', 'Menu.target', 'Language.ISO-639-2'),
            'order' => array('Menu.lft'),
            'recursive' => 0));
        $this->set(compact('menusTopUser'));

我的关联看起来像:

Menu
hasMany: MenusUserGroup

MenusUserGroup
belongsTo: Menu, UserGroup

执行find后,我有下一个数组,其中有许多MenusUserGroup为空。如何使用' MenusUserGroup.user_group_id'来过滤记录。 => $这 - > Auth->用户(' UserGroup.id&#39)

Array(
[0] => Array
    (
        [Menu] => Array
            (
                [title] => Offerte e modulistica promoter
                [url] => 
                [target] => _self
                [id] => 11
            )

        [Item] => Array
            (
                [slug] => offerte-e-modulistica-promoter
                [id] => 36
            )

        [Language] => Array
            (
                [ISO-639-2] => ita
                [id] => 1
            )

        [TemplateRegion] => Array
            (
                [id] => 10
            )

        [MenusUserGroup] => Array
            (
            )

    )

[1] => Array
    (
        [Menu] => Array
            (
                [title] => Offerte e modulistica agenti
                [url] => 
                [target] => _self
                [id] => 10
            )

        [Item] => Array
            (
                [slug] => offerte-modulistica
                [id] => 29
            )

        [Language] => Array
            (
                [ISO-639-2] => ita
                [id] => 1
            )

        [TemplateRegion] => Array
            (
                [id] => 10
            )

        [MenusUserGroup] => Array
            (
                [0] => Array
                    (
                        [id] => 7
                        [menu_id] => 10
                        [user_group_id] => 5
                    )

            )

    )

[2] => Array
    (
        [Menu] => Array
            (
                [title] => Gestione contratti
                [url] => 
                [target] => _self
                [id] => 13
            )

        [Item] => Array
            (
                [slug] => 
                [id] => 
            )

        [Language] => Array
            (
                [ISO-639-2] => ita
                [id] => 1
            )

        [TemplateRegion] => Array
            (
                [id] => 10
            )

        [MenusUserGroup] => Array
            (
                [0] => Array
                    (
                        [id] => 10
                        [menu_id] => 13
                        [user_group_id] => 5
                    )

            )

    )

1 个答案:

答案 0 :(得分:0)

使用联接。

 $menusTopUser = $this->Menu->find('all', array(
        // join statement
        'joins' => array(
            array(
                'table' => 'menus_user_groups',
                'alias' => 'MenuUserGroupJoin',
                'type' => 'INNER',
                'conditions' => array(
                    'MenuUserGroupJoin.menu_id = Menu.id',
                    // condition
                    'MenuUserGroupJoin.user_group_id = ' . $this->Auth->user('UserGroup.id'),
                ),
            ),
        ),
        'contain' => array('TemplateRegion', 'Language', 'Item', 'MenusUserGroup'),
        'conditions' => array(
            'TemplateRegion.alias' => 'menu_top_user',
            'Menu.active' => 1,
            'Language.ISO-639-2' => $this->Session->read('Config.frontendLanguage'),
        ),
        'fields' => array('Menu.title', 'Item.slug', 'Menu.url', 'Menu.target', 'Language.ISO-639-2'),
        'order' => array('Menu.lft'),
        'recursive' => 0));