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.
精彩评论