Dear,
I have an issue about inserting many records (getting from json format)
Here is my code
if ($this->Scans->save($scan))
{
for ($i = 0; $i < count(($json[0][“articles”])); $i++)
{
$detail = $this->Details->newEmptyEntity();
$detail->idScan = $idScan;
$detail->ean = strval($json[0][“articles”][$i][“ean”]);
$detail->qty = intval($json[0][“articles”][$i][“qte”]);
$this->Details->save($detail);
}
}
I have three records and it only save 1 record.
I tried to debug by displaying all $detail->ean in the loop and it works.
Do you have any ideas ?
Thanks in advance.
Do you look at the results of the save calls to see if there are maybe validation errors? But beside that, why not patch your $scan
with the data for the articles to get the full structure in that entity and then save them all in a single transactional call?
Thanks for your answer Zuluru.
Yes I checked the result and there are no errors.
I tried your method too and use saveMany() instead of save() but the issue is still the same.
for ($i = 0; $i < count(($json[0][“articles”])); $i++)
{
$data[$i][“idScan”] = $idScan;
$data[$i][“ean”] = strval($json[0][“articles”][$i][“ean”]);
$data[$i][“qty”] = intval($json[0][“articles”][$i][“qte”]);
}
$detail = $this->Details->newEntities($data);
$result = $this->Details->saveMany($detail);
Each time, only the last record is saved.
Can you include the output of debug($detail)
both before and after the saveMany
call?
oh I found my error…
I set idScan as primary key into ScansTable.php … so it didnt add two records with the same idScan…
But it’s weird, even if I add validation on save() method, the code still works.