2012-05-25 44 views
0

我正在嘗試創建一個簡單的請求/接受函數。用戶添加一個人 - 兩個用戶的用戶名都與自動生成的ID一起添加到表中。現在,當其他用戶檢查他們的請求時,他們會點擊批准並將有效日期添加到記錄中。目前發生的情況是,當用戶點擊接受並設置到期日期時,它會在關係表中創建一條新記錄。更新cakephp中的一行

我的關係表的結構是

id 
partyone 
partytwo 
active 
expirydate 

公共函數add(){

 if($this->request->is('post')){ 
     $this->Relationship->create(); 
     if ($this->Relationship->save($this->request->data)) 
     { 
      $id=$this->Relationship->id; 
      $this->Session->setFlash('The relationship has been saved'); 

     } 
     else { $this->Session->setFlash('The relationship could not be saved. Please, try again.'); } 
     } 

     } 



     public function approve(){ 
      if($this->request->is('post')){ 
      $this->Relationship->id; 
      $this->Relationship->getLastInsertID(); 
      $this->Relationship->save($this->request->data); 
      $this->Session->setFlash('The information has been saved');} 
      else{ 
      $this->Session->setFlash('The information couldnt be saved');} 


     } 


    } 

這裏是我的批准觀點

<?php 
echo $this->Form->create('Relationship', array('action'=>'approve')); 
echo $this->Form->input('expirydate',array('label'=>'Expiry Date: ', 'class' => 'dateclass')); 
echo $this->Form->end('Submit'); 

?> 

這裏是我的外接觀點

<?php 
echo $this->Form->create('Relationship', array('action'=>'add')); 
echo $this->Form->input('partyone',array('label'=>'Enter your username: ')); 
echo $this->Form->input('partytwo',array('label'=>'Username of user: ')); 
echo "<br />"; 
echo $this->Form->end('Click here to add relationship'); 

?> 

我該如何編碼,以便它更新並且不會創建帶有失效日期的新行,請幫助我對此失去理智。

回答

0

這不是做任何事情。這是一個錯字:

 $this->Relationship->id; 
     $this->Relationship->getLastInsertID(); 

應該明顯讀:

 $this->Relationship->id = $this->Relationship->getLastInsertID(); 
+0

這將不起作用,因爲'getLastInsertID()'將返回null,因爲還沒有插入任何內容。 – dhofstet

0

您需要提供更多的信息,但其基本思想是:

在您approve視圖,你需要有存儲的idrelationship待批准。

您可以將ID作爲approve方法的參數存儲,或作爲隱藏字段存儲。

function approve($id=null) { 
    /*Form action = Relationship/Approve/ID*/ 
    $this->Relationship->id = $id; 
    /*or - hidden field/an input*/ 
    $this->Relationship->id = $this->request->data['Relationship']['id']; 
    $this->Relationship->save($this->request->data); 
} 

提供id在保存之前設置,Cake會更新,而不是添加。

+0

其投擲未定義索引:與$ this-> request-> data的關係['Relationship'] ['id']; 它不喜歡方括號中的內容。我以爲你應該把字段放在表中 – user1393064

+0

表中的字段* – user1393064