0

谁能向我解释为什么当我执行以下代码时 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 个单独的行

编辑:

在我遵循以下解决方案并将代码更改为此之后:

$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”中。这怎么可能?

4

2 回答 2

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'));
于 2012-09-20T08:54:19.763 回答
0

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

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

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

于 2012-09-20T08:54:58.057 回答