# Insert many records only inserts one record

**URL:** <https://discourse.cakephp.org/t/insert-many-records-only-inserts-one-record/9396>\
**Category:** Need Help\
**Created:** [June 2, 2021, 12:28pm UTC](https://discourse.cakephp.org/t/insert-many-records-only-inserts-one-record/9396 "2021-06-02T12:28:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nicocins](https://avatars.discourse-cdn.com/v4/letter/n/82dd89/32.png) [@nicocins](https://discourse.cakephp.org/u/nicocins)\
**Post date:** [June 2, 2021, 12:28pm UTC](https://discourse.cakephp.org/t/insert-many-records-only-inserts-one-record/9396/1 "2021-06-02T12:28:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [June 2, 2021, 1:51pm UTC](https://discourse.cakephp.org/t/insert-many-records-only-inserts-one-record/9396/2 "2021-06-02T13:51:53Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![nicocins](https://avatars.discourse-cdn.com/v4/letter/n/82dd89/32.png) [@nicocins](https://discourse.cakephp.org/u/nicocins)\
**Post date:** [June 2, 2021, 1:55pm UTC](https://discourse.cakephp.org/t/insert-many-records-only-inserts-one-record/9396/3 "2021-06-02T13:55:35Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [June 2, 2021, 5:28pm UTC](https://discourse.cakephp.org/t/insert-many-records-only-inserts-one-record/9396/4 "2021-06-02T17:28:08Z")

</div>

Can you include the output of `debug($detail)` both before and after the `saveMany` call?

---

<div class="post-metadata">

**Author:** ![nicocins](https://avatars.discourse-cdn.com/v4/letter/n/82dd89/32.png) [@nicocins](https://discourse.cakephp.org/u/nicocins)\
**Post date:** [June 3, 2021, 12:04pm UTC](https://discourse.cakephp.org/t/insert-many-records-only-inserts-one-record/9396/5 "2021-06-03T12:04:51Z")

</div>

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.
