2在一个型号中保存呼叫

时间:2012-09-20 08:52:01

标签: cakephp cakephp-2.1

任何人都可以向我解释为什么CakePHP只会在执行以下代码时创建一行

$this->Adresses->save(array('Contact_ID' => $ClientID, 'Type' => 1, 'Addressline_1' =>    $_POST['Straat'], 'House_Number' => $_POST['Huisnummer'], 'Postal_Code' => $_POST['Postcode'], 'City' => $_POST['Woonplaats'], 'Country' => 'NL'));

$this->Adresses->save(array('Contact_ID' => $ClientID, 'Type' => 2, 'Addressline_1' => $_POST['F_Straat'], 'House_Number' => $_POST['F_Huisnummer'], 'Postal_Code' => $_POST['F_Postcode'], 'City' => $_POST['F_Woonplaats'], 'Country' => 'NL'));

我只想让它在同一张表中制作2个单独的行

编辑:

在我按照下面的解决方案并将te代码更改为:

$this->Adresses->create();

$this->Adresses->save(array('Contact_ID' => $ClientID, 'Type' => 1, 'Addressline_1' =>   $_POST['Straat'], 'House_Number' => $_POST['Huisnummer'], 'Postal_Code' => $_POST['Postcode'], 'City' => $_POST['Woonplaats'], 'Country' => 'NL'));

$this->Adresses->create();

$this->Adresses->save(array('Contact_ID' => $ClientID, 'Type' => 2, 'Addressline_1' => $_POST['F_Straat'], 'House_Number' => $_POST['F_Huisnummer'], 'Postal_Code' => $_POST['F_Postcode'], 'City' => $_POST['F_Woonplaats'], 'Country' => 'NL'));

它保存到单独的行,但由于某种原因,列类型在两行'1'中。这怎么可能?

2 个答案:

答案 0 :(得分:1)

您应该在第一个create方法后调用save方法 例如: -

$this->Adresses->save(array('Contact_ID' => $ClientID, 'Type' => 1, 'Addressline_1' =>    $_POST['Straat'], 'House_Number' => $_POST['Huisnummer'], 'Postal_Code' => $_POST['Postcode'], 'City' => $_POST['Woonplaats'], 'Country' => 'NL'));

$this->Adresses->create();

$this->Adresses->save(array('Contact_ID' => $ClientID, 'Type' => 2, 'Addressline_1' => $_POST['F_Straat'], 'House_Number' => $_POST['F_Huisnummer'], 'Postal_Code' => $_POST['F_Postcode'], 'City' => $_POST['F_Woonplaats'], 'Country' => 'NL'));

答案 1 :(得分:0)

因为您在创建新记录时忘记了最基本的事情之一:始终先调用create():

 $this->Adresses->create();
 $this->Adresses->save($data);

否则第二个调用将使用第一个调用的id。所以永远不要忘记创建新记录时的create()(而不是更新现有记录)。