将ID保存到会话

问题描述:

我想在保存产品后将产品ID放入会话中,但是当我阅读会话时遇到错误的ID。将ID保存到会话

foreach ($this->data['Address'] as $key => $ab) { 
    $this->data['Product'][$key]['model'] = 'User'; 
    $this->data['Product'][$key]['foreign_key'] = $this->User->id; 
    $this->Supplier->Brand->Type->create(); 
    $this->Supplier->Brand->Type->save($this->data['Product'][$key]); 
    $this->Session->write('productId', $this->Supplier->Brand->Type->id); 
} 

如何获取产品ID?

+0

要保存在集合中的最后一个ID,因为每个连续的循环覆盖前一个。这不是你想要的吗? – JJJ 2012-04-11 15:03:44

+0

是的,我想得到最后一个ID .... – vnpnlz 2012-04-11 15:05:36

尝试用代码

foreach ($this->data['Address'] as $key => $ab) 
{ 
$this->data['Product'][$key]['model'] = 'User'; 
$this->data['Product'][$key]['foreign_key'] = $this->User->id; 
$this->Supplier->Brand->Type->create(); 
$this->Supplier->Brand->Type->save($this->data['Product'][$key]); 

$lastid = $this->Supplier->Brand->Type->getLastInsertID() 

$this->Session->write('productId', $lastid); 
}