开发者

CakePHP: Filter Index() with session's user ID

I have a small cakePHP app that allows for user's to login and be provided a session to give their status on an assigned project.

I am replacing the baked view portion of the add() function that asks which user to create a new status report for under with this line of code in the controller:

$this->data['Status']['user_id'] = $this->Auth->user('id');

I would like to do something similar in the index() function so that users could开发者_如何转开发 also, only see the their previous status reports and not any other users.

How would I attempt to filter the index() function?

function index() {
    $this->Status->recursive = 0;
    $this->set('statuses', $this->paginate());
}


Simply set your conditions in the paginate variable:

function index() {
    $this->paginate = array(
        'recursive' => 0,
        'conditions' => array('Status.user_id' => $this->Auth->user('id')),
    );
    $this->set('statuses', $this->paginate('Status'));
}


You'll want to look at setting custom pagination queries. The cakePHP book (linked) is a great resource for this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜