しばらく時間がかかりましたが、ようやくこれが機能するようになりました。ここにあります:
私の 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('/');
}
これが誰にも役立つことを願っています..