我花了一段时间,但我终于得到了这个工作。这里是:
在我的 AppController 中,我将用户重定向到我的项目控制器 afterlogin() 操作。
public $components = array(
'Auth'=>array(
'loginRedirect'=>array(
'controller' => 'items', 'action' => 'afterlogin'),
)));
然后在我的 ItemsController 中,我添加了 afterlogin() 函数:
public function afterlogin(){
$favors = $this->Session->read('favorites');//get favorites stored in Session
$this->loadModel('Item2user');//Load my join-table model
foreach ($favors as $favor){
//search for existing favorites
$found = $this->Item2user->find('all', array(
'conditions' => array('item_id =' => $favor['item_id'],
'user_id =' => $this->Auth->user('id'))
));
if($found){
//the entry is already in the db, ->do nothing
}
else {
//now save the entry to the join-table
$this->Item->create(array(
'Item' => array('id' => $favor['item_id']),
'User' => array('id' => $this->Auth->user('id'))
));
$this->Item->save();
}
}
$this->redirect('/');
}
希望这对任何人都有帮助..