2016-09-26 53 views
0

这是我更新的代码: PS:EMPID是一个外键,但我认为不应该是理性和代码在CakePHP的我想更新,但它是增加一个新的行

if($this->request->is('post')) 
     { 
      $this->request->data["Leave"]["empid"] = $this->request->data["id"]; 
      $this->Leave->empid = $this->request->data["Leave"]["empid"]; 
      $this->request->data["Leave"]["leave_start"] = $this->request->data["start_date"]; 
      $this->request->data["Leave"]["leave_end"] = $this->request->data["end_date"]; 
      $this->request->data["Leave"]["leave_taken"] = $this->request->data["leave_taken"]; 

      if($this->Leave->save($this->request->data['Leave'])) 
      { 
       return $this->redirect(array('action' => 'manage_leave')); 
      } 
     } 

//此代码插入新行而不是更新,也不会在新行中添加任何值

+0

如果您代表外键进行更新,那么您需要使用updateAll方法而不是保存。 如果你是代表主键进行更新,那么你需要传递主键保存数据 –

回答

0

可能是您试图使用simple save来更新外部表数据。外键

Model::updateAll(array $fields, mixed $conditions) 

更新多条记录例

$this->Ticket->updateAll(
    array('Ticket.status' => "'closed'"), 
    array('Ticket.customer_id' => 453) 
); 

简单节省的主键

确保您的HTML有empid

echo $this->Form->input('Leave.empid', array('type' => 'hidden')); 

保存模型

$this->Leave->empid = $this->request->data["Leave"]["empid"]; //2 
$this->Leave->save($this->request->data); 

在这两者之间,你也可以尝试set模型数据,并检查$this->Leave->validates()$this->Leave->validationError如果他们给任何验证错误。

// Create: id isn't set or is null 
$this->Recipe->create(); 
$this->Recipe->save($this->request->data); 

// Update: id is set to a numerical value 
$this->Recipe->id = 2; 
$this->Recipe->save($this->request->data); 

你可以找到所有Saving your data

希望更多的信息,这可以帮助你:)

+0

感谢用户,其工作(y) –

+0

@AdityaManhas很高兴看到,如果它帮助你,然后接受答案。 –

0

而且万一要是$ EMPID是离开模型的相应表的主键(如叶),只是更换:

$this->Leave->empid = $this->request->data["Leave"]["empid"]; 

通过

$this->Leave->id = $this->request->data["Leave"]["empid"]; 
+0

其工作,谢谢 –

相关问题