Error save record from table to another table

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

$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

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

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

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.

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.

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

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

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