# Error save record from table to another table

**URL:** <https://discourse.cakephp.org/t/error-save-record-from-table-to-another-table/8640>\
**Category:** Need Help\
**Created:** [November 21, 2020, 3:42am UTC](https://discourse.cakephp.org/t/error-save-record-from-table-to-another-table/8640 "2020-11-21T03:42:39Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![wakikuk](https://avatars.discourse-cdn.com/v4/letter/w/e95f7d/32.png) [@wakikuk](https://discourse.cakephp.org/u/wakikuk)\
**Post date:** [November 21, 2020, 3:42am UTC](https://discourse.cakephp.org/t/error-save-record-from-table-to-another-table/8640/1 "2020-11-21T03:42:39Z")

</div>

Sorry, Hi I Need Help, I want to Save multiple record from table to another table with Contain or Associated record, Here my Code 😄:

> $tbltrxsortidata = TableRegistry::getTableLocator()-\>get(‘Trxsortirdata’);  
> $dataperiode = $tbltrxsortidata-\>find()-\>where([‘sesi\_periode’=\>$sesi\_periodeclose])-\>contain([‘SubtrxFile’]);  
> // return $this-\>response-\>withType(‘application/json’)-\>withStringBody(json\_encode($dataperiode-\>toArray()));  
> //INSERT DATA yang akan di Close  
> $tblfiletrx = TableRegistry::getTableLocator()-\>get(‘ArsipTrxsortirdata’);  
> $entities = $tblfiletrx-\>newEntities($dataperiode-\>toArray(),[‘associated’=\>[‘ArsipSubtrxfile’=\>[‘validate’ =\> false]]]);  
> $patcentities = $tblfiletrx-\>patchEntities($entities, $dataperiode-\>toArray(),[‘associated’=\>[‘ArsipSubtrxfile’=\>[‘validate’ =\> false]]]);  
> // return $this-\>response-\>withType(‘application/json’)-\>withStringBody(json\_encode($patcentities));  
> //Salin Data  
> $tblfiletrx-\>saveMany($entities)

What Fail with my Code ? Must Change name key association Same with Name On Association another Table ? Please help me

---

<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:** [November 21, 2020, 3:58am UTC](https://discourse.cakephp.org/t/error-save-record-from-table-to-another-table/8640/2 "2020-11-21T03:58:06Z")

</div>

Are your Trxsortirdata and ArsipTrxsortirdata tables identical in structure, and you’re trying to copy records from one to the other?

---

<div class="post-metadata">

**Author:** ![wakikuk](https://avatars.discourse-cdn.com/v4/letter/w/e95f7d/32.png) [@wakikuk](https://discourse.cakephp.org/u/wakikuk)\
**Post date:** [November 21, 2020, 4:08am UTC](https://discourse.cakephp.org/t/error-save-record-from-table-to-another-table/8640/3 "2020-11-21T04:08:18Z")

</div>

yes, Its identical all field same, yes i want to copy record, I want to do that for backup or archive record and close the periode

---

<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:** [November 21, 2020, 4:37pm UTC](https://discourse.cakephp.org/t/error-save-record-from-table-to-another-table/8640/4 "2020-11-21T16:37:30Z")

</div>

I’m not sure the best way to do that. Have you tried simply `$tblfiletrx->saveMany($dataperiode->toArray())`?

If that doesn’t work, I think the problem with your current code is that `$dataperiode->toArray()` will be returning an array of entities, which I don’t believe `newEntitites` or `patchEntities` will like; they expect their data as arrays of arrays. (Side note, if you pass data to `newEntities`, you don’t need to also patch them.) You might need to create the new entities in a loop over `$dataperiode`, and then calling `toArray` on each entity.

---

<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:** [November 21, 2020, 4:38pm UTC](https://discourse.cakephp.org/t/error-save-record-from-table-to-another-table/8640/5 "2020-11-21T16:38:52Z")

</div>

Alternately, consider changing the structure, so that you always leave all the records in the one table, and just exclude previous periods from queries where you are loading them.

---

<div class="post-metadata">

**Author:** ![wakikuk](https://avatars.discourse-cdn.com/v4/letter/w/e95f7d/32.png) [@wakikuk](https://discourse.cakephp.org/u/wakikuk)\
**Post date:** [November 22, 2020, 11:44am UTC](https://discourse.cakephp.org/t/error-save-record-from-table-to-another-table/8640/6 "2020-11-22T11:44:44Z")

</div>

Thank brother, the record on contain its dynamically update, so if i exclude without clone, the record will take new modfied, and the informatin will change, i try clone or insert from one to another table with one by one table the query except id, created and modified, and with saveMany, there all sucessfull, but iam cofusing how to merger data, from old file to new file,  
Example,  
I have record with sesi\_periode 2018/2019 with contain or assosciated subrecord or list item, i want close periode with logic : backup record and contain to another table and update record to new sesi\_periode ex 2019/2020, and before i had record with sesi\_periode 2019/2020 with code file not same, how to merge it, i want contain or record on relation from sesi\_periode 2018/2019 combine or merger with sesi\_periode 2019/202020

---

<div class="post-metadata">

**Author:** ![wakikuk](https://avatars.discourse-cdn.com/v4/letter/w/e95f7d/32.png) [@wakikuk](https://discourse.cakephp.org/u/wakikuk)\
**Post date:** [November 22, 2020, 11:56am UTC](https://discourse.cakephp.org/t/error-save-record-from-table-to-another-table/8640/7 "2020-11-22T11:56:23Z")

</div>

![Screenshot_20201122-185535](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/c/c499b24e0b82c53b2c58032f7448279c8b5180a4.jpeg) ![Screenshot_20201122-185528](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/9/96e572a355b531b63fb4b978a302898b4e203bf0.jpeg) ![Screenshot_20201122-185436](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/9/94262906bc8e7ea462543a42eabe4c9749bca2de.jpeg) ![Screenshot_20201122-185344](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/2/296af59f061e911bfa02a289c899ad275a7848e4.jpeg) ![Screenshot_20201122-185332](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/a/a87a77eae01d7e7512b9e134482fc2d37fb4aa03.jpeg)

---

<div class="post-metadata">

**Author:** ![wakikuk](https://avatars.discourse-cdn.com/v4/letter/w/e95f7d/32.png) [@wakikuk](https://discourse.cakephp.org/u/wakikuk)\
**Post date:** [November 22, 2020, 12:39pm UTC](https://discourse.cakephp.org/t/error-save-record-from-table-to-another-table/8640/8 "2020-11-22T12:39:20Z")

</div>

And Please help, how to use updateAll with condition from associated table?  
UpdateAll with condition macthing from associated table

---

<div class="post-metadata">

**Author:** ![wakikuk](https://avatars.discourse-cdn.com/v4/letter/w/e95f7d/32.png) [@wakikuk](https://discourse.cakephp.org/u/wakikuk)\
**Post date:** [November 22, 2020, 12:40pm UTC](https://discourse.cakephp.org/t/error-save-record-from-table-to-another-table/8640/9 "2020-11-22T12:40:57Z")

</div>

$table-\>updateAll([set] , [condition wih macthing associated table] )
