2016-01-25 15 views
0

我想編輯數據,但這裏這個編輯數據將是一個新條目,我們將刪除舊數據。Cakephp編輯數據作爲一個新條目

所以我曾嘗試下面的代碼來做到這一點

public function edit($id = null){ 
if ($this->request->is(array('post', 'put'))) 
{ 
    $this->Transaction->create(); 
    if ($this->Transaction->save($this->request->data)) { 
       $this->Transaction->delete(); 
       $this->Session->setFlash(__('The transaction has been saved.')); 
    } 

} 

這裏只刪除發生,我沒有得到新記錄數據。我怎樣才能做到這一點?請有人幫助我嗎?

回答

0

IN窗體編輯首先將您的ID分配到模型主鍵中。並需要形成create()方法,並檢查你的視圖源PUT方法是否可用。

public function edit($id = null){ 
if (!$this->Transaction->exists($id)) { 
     throw new NotFoundException(__('Invalid user')); 
    } 
if ($this->request->is(array('post', 'put'))){ 
    $this->Transaction->id=$id; 
    if ($this->Transaction->save($this->request->data)) { 
      $this->Session->setFlash(__('The transaction has been saved.')); 
    } 
    } 

另一個替代方法是updateAll() Cakephp updateAll

+0

差異在哪裏?在這裏,我試圖將數據更新爲新條目並刪除舊數據。 –

+0

$ id與您的Model主鍵相關 –

+0

我已經添加了這個異常,我的查詢是,是否可以編輯數據來創建一個新條目? –

0

必須有很多方法可以做到這一點。讓我們看看其中一種方式

public function edit(){ 

    if ($this->request->is(array('post', 'put'))) 
    { 
     $this->Transaction->delete($this->request->params['pass'][0]); // after post at first delete your entry. 

     if ($this->Transaction->save($this->request->data)) { 
        $this->Session->setFlash(__('The transaction has been saved.')); 
     } 

    }else { 
     //to get this id data. 
     $id = $this->request->params['pass'][0]; 
     $options = array('conditions' => array('Transaction.' . $this->Transaction->primaryKey => $id)); 
     $this->request->data = $this->Transaction->find('first', $options); 
    } 

我thik它可以幫助你。

相關問題