0

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

我有的是这个

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

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

4

1 回答 1

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,
        )
    )
);
于 2013-11-11T18:46:05.423 回答