Cakephp2包含深层关联

时间:2013-11-11 18:04:03

标签: php cakephp pagination cakephp-2.0 containable

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

我拥有的是这个

//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(分配给我的项目)

这样的事情。 我试图使用可包含但它不起作用,也许我做的方式有问题?

1 个答案:

答案 0 :(得分:0)

你的包含的条件应该是这样的:

$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,
        )
    )
);