I try to receive the data of associated models to the current logged in user. This is my current User model:
class User extends AppModel {
public $name = 'User';
public $hasMany = array(
'Website' => array(
'className' => 'Website',
'foreignKey' => 'user_id'
)
);
}
And this is the Website model which is associated to the User model:
class Website extends AppModel {
public $name = 'Website';
public $belongsTo = array(
'User' => array(
'classname' => 'User',
'foreignKey' => 'user_id'
)
);
}
When I try to get the websites of the logged in user I don't want to set the user_id of the website in a static way in the conditions of the DB request. So I tried to use the associations:
public function beforeFilter() {
parent::beforeFilter();
$this->Website->User->id = $this->Auth->user('id');
// OR
$this->Website->User->set($this->Auth->user());
}
public function index() {
$this->set('websites', $this->Website->find('all'));
}
But it returns all websites instead of only the ones with the correct user_id.
Is there a way to do it the way i suggest? If yes, what am I doing wrong?
Many thanks!