cakephp:更新相关的belogsTo表

时间:2014-07-31 12:00:39

标签: cakephp model-associations

我正在更新属于地址模型的零售商模型,当我创建新记录时,它可以轻松地为地址创建新行并将其密钥插入零售商模型:

  

查看:

echo $this->Form->create('Retailer');
echo $this->Form->input('Name');
echo $this->Form->input('Address.add_line1');
echo $this->Form->input('Address.add_line2');
echo $this->Form->end('Submit');
  

模特(零售商):

public $belongsTo = 'Address';
  

RetailersController->添加()

$this->Retailer->create();
$this->Retailer->saveAll($this->request->data);

然而,当我尝试更新零售商时,零售商模型已更新,但是更新了以前的地址记录,它创建了新记录并将其密钥放在零售商表上:

RetailersController->edit($id = null)
$this->Retailer->id = $id;
$this->Retailer->saveAll($this->request->data);

我也试过这个但没有工作:

RetailersController->edit($id = null)
$post = $this->Retailer->findById($id);
$this->Retailer->id = $id;
$this->request->data['Retailer']['address_id'] = $post['Retailer']['address_id'];
$this->Retailer->saveAll($this->request->data);

1 个答案:

答案 0 :(得分:0)

您可能只需要告诉它地址的ID。

echo $this->Form->input('Address.id');

否则它将执行创建,正如您所说。