【问题标题】:Cakephp2 Contain Deeper AssociationCakephp2 包含更深的关联
【发布时间】:2013-11-23 15:21:05
【问题描述】:

我需要对表格进行分页,我只需要查看分配给我的项目,我该如何实现?

我有的是这个

//My Task Pagination
$this->paginate = array(
    'Project' => array(
        'limit' => $limit,
        'contain' => array('ProjectReminderUser.user_id' => $this->Session->read('Auth.User.id')),
        'conditions' => array(
            'User.group_id' => $this->Session->read('Auth.User.group_id'),
            'Project.project_status_id' => PROJECT_STATUS_OPEN,
        ),
    )
);

$this->set('myTasks', $this->paginate('Project'));  
//debug($this->paginate('Project'));

但似乎包含根本不起作用。调试的结果是这样的

array(
    (int) 0 => array(
        'Project' => array(
            'id' => '9',
            'project_type_id' => '0',
            'contact_id' => '2',
            'company_id' => '6',
        ),
        'ProjectReminderUser' => array(
            (int) 0 => array(
                'id' => '11',
                'user_id' => '2',
                'project_id' => '9'
            )
        ),
    (int) 1 => array(
        'Project' => array(
            'id' => '1',
            'project_type_id' => '0',
            'contact_id' => '1',
            'company_id' => '3',
        ),
        'ProjectReminderUser' => array(
            (int) 0 => array(
                'id' => '2',
                'user_id' => '1',
                'project_id' => '1'
            ),
            (int) 1 => array(
                'id' => '1',
                'user_id' => '2',
                'project_id' => '1'
            )
        ),
    )
)

所以我想要实现的是让分页只显示数组索引 1,因为 ProjectReminderUser 有 user_id = 1(分配给我的项目)

类似的东西。 我尝试使用可包含但它不起作用,可能是我这样做的方式有问题?

【问题讨论】:

    标签: php cakephp pagination cakephp-2.0 containable


    【解决方案1】:

    你的容器条件应该是这样的:

    $this->paginate = array(
        'Project' => array(
            'limit' => $limit,
            'contain' => array(
                'ProjectReminderUser' => array(
                    'conditions' => array(
                         'ProjectReminderUser.user_id' => $this->Session->read('Auth.User.id')
                    )
                 )
            ),
            'conditions' => array(
                'User.group_id' => $this->Session->read('Auth.User.group_id'),
                'Project.project_status_id' => PROJECT_STATUS_OPEN,
            )
        )
    );
    

    如果你想包含用户,它应该是这样的:

    $this->paginate = array(
        'Project' => array(
            'limit' => $limit,
            'contain' => array(
                'ProjectReminderUser' => array(
                    'conditions' => array(
                         'ProjectReminderUser.user_id' => $this->Session->read('Auth.User.id')
                    )
                 ),
                 'User' => array(
                     'conditions' => array(
                         'User.group_id' => $this->Session->read('Auth.User.group_id'),
                     )
                 )
            ),
            'conditions' => array(
                'Project.project_status_id' => PROJECT_STATUS_OPEN,
            )
        )
    );
    

    还要确保您在所有涉及的模型中都有行为。

    如果需要使用连接:

    $this->paginate = array(
        'Project' => array(
            'limit' => $limit,
            'joins' => array(
                'table' => 'project_reminder_users',
                'alias' => 'ProjectReminderUser',
                'type' => 'inner',
                'conditions' => array(
                    'ProjectReminderUser.user_id = ' . $this->Session->read('Auth.User.id'),
                )
            ),
            'conditions' => array(
                'User.group_id' => $this->Session->read('Auth.User.group_id'),
                'Project.project_status_id' => PROJECT_STATUS_OPEN,
            )
        )
    );
    

    【讨论】:

    • @vicomacho 使用这种方式,它仍然会返回所有数组,它只是 ProjectReminderUser 将是空数组,我应该怎么做才能让空数组的结果没有返回?
    • @Harts 不幸的是,这是“可包含”的预期行为。我所做的只是在分页/查找中指定我自己的联接 (book.cakephp.org/2.0/en/models/…),它使这些类型的查询变得更加轻松。
    • @iso27002 是对的,更新了我的答案以展示如何执行此操作的示例。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多