ZéSá
ZéSá

Reputation: 19

CakePHP using Session outside of the function

I have a piece of code that is responsible for the pagination in my controllers:

var $paginate = array(
            'limit' => 5,
            'conditions' => array('Tanque.user_id' => 1),
            'order' => array(
            'Tanque.nome' => 'asc'
        )
    );

So, where I have 'Tanque.user_id' => 1, I would like to use the user_id instead of the number 1. I am getting the user_id from the Session $this->Session->read('Auth.User.id'), but I get an error.

How can I use the $this->Session->read outside of the functions?

Upvotes: 0

Views: 157

Answers (1)

deceze
deceze

Reputation: 522081

You cannot use expressions in class declarations, you can only use static values. Not to mention that all the components aren't even loaded at this stage. Do this in a function, like beforeFilter:

public function beforeFilter() {
    $this->paginate['conditions']['Tanque.user_id'] = $this->Auth->user('id');
    parent::beforeFilter();
}

Upvotes: 1

Related Questions