2012-04-11 91 views
1

我想在保存產品後將產品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

回答

0

嘗試用代碼

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); 
}