Hello, im trying to figure out why the save() with associated option not working, when i create the main entity and the associated one from a form and save it works, but when i try loading it from database changing some data and saving only the main entity data is saved.
- cake version 3.6.3;
- disabled validate and checkRules for both;
- debug shows the correct modified data with fields marked as dirty for both;
- save() return true but only the main entity data is saved (associated data still marked as dirty after save);
i don’t know where to look the problem as there are no erros
save function:
public function baixar($id = null)
{
if (!$id) {
throw new NotFoundException(__('Invalid ID'));
}
$recibo = $this->Recibos->get($id, ['contain' => ['Pagamentos']]);
if ($this->request->is(['post', 'put'])) {
$recibo->recibo_situacao_id = 2;
$recibo->data_baixa = Chronos::now();
foreach ($recibo->pagamentos as $pagamento) {
$pagamento->pagamento_situacao_id = 2;
}
debug($recibo); //generates correct data with the dirty fields of the main entity and the associated one
if ($this->Recibos->save($recibo, [
'associated' => [
'Pagamentos' => ['validate' => false, 'checkRules' => false]
],
'validate' => false,
'checkRules' => false
])) {
debug($recibo);
} else {
debug('error');
}
}
}
entity debug before and after save:
//debug before save()
/src/Controller/Admin/RecibosController.php (line 185)
object(App\Model\Entity\Recibo) {
'id' => (int) 37,
'pagamento_tipo_id' => (int) 1,
'recibo_situacao_id' => (int) 2,
'data_baixa' => object(Cake\Chronos\Chronos) {
'time' => '2018-05-16 13:46:18.978997',
'timezone' => 'America/Sao_Paulo',
'hasFixedNow' => false
},
'pagamentos' => [
(int) 0 => object(App\Model\Entity\Pagamento) {
'id' => (int) 68,
'pagamento_situacao_id' => (int) 2,
'recibo_id' => (int) 37,
'valor' => (float) 72,
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'pagamento_situacao_id' => true
],
'[original]' => [
'pagamento_situacao_id' => (int) 1
],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Pagamentos'
},
(int) 1 => object(App\Model\Entity\Pagamento) {
'id' => (int) 69,
'pagamento_situacao_id' => (int) 2,
'recibo_id' => (int) 37,
'valor' => (float) 342,
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'pagamento_situacao_id' => true
],
'[original]' => [
'pagamento_situacao_id' => (int) 1
],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Pagamentos'
},
(int) 2 => object(App\Model\Entity\Pagamento) {
'id' => (int) 70,
'pagamento_situacao_id' => (int) 2,
'recibo_id' => (int) 37,
'valor' => (float) 100,
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'pagamento_situacao_id' => true
],
'[original]' => [
'pagamento_situacao_id' => (int) 1
],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Pagamentos'
}
],
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'recibo_situacao_id' => true,
'data_baixa' => true
],
'[original]' => [
'data_baixa' => object(Cake\I18n\FrozenTime) {
'time' => '2018-05-16T13:43:57-03:00',
'timezone' => 'America/Sao_Paulo',
'fixedNowTime' => false
}
],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Recibos'
}
//debug after save(), the only diference is the main entity dirty array is empty because of the save()
/src/Controller/Admin/RecibosController.php (line 191)
object(App\Model\Entity\Recibo) {
'id' => (int) 37,
'pagamento_tipo_id' => (int) 1,
'recibo_situacao_id' => (int) 2,
'data_baixa' => object(Cake\Chronos\Chronos) {
'time' => '2018-05-16 13:46:18.978997',
'timezone' => 'America/Sao_Paulo',
'hasFixedNow' => false
},
'pagamentos' => [
(int) 0 => object(App\Model\Entity\Pagamento) {
'id' => (int) 68,
'pagamento_situacao_id' => (int) 2,
'recibo_id' => (int) 37,
'valor' => (float) 72,
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'pagamento_situacao_id' => true
],
'[original]' => [
'pagamento_situacao_id' => (int) 1
],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Pagamentos'
},
(int) 1 => object(App\Model\Entity\Pagamento) {
'id' => (int) 69,
'pagamento_situacao_id' => (int) 2,
'recibo_id' => (int) 37,
'valor' => (float) 342,
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'pagamento_situacao_id' => true
],
'[original]' => [
'pagamento_situacao_id' => (int) 1
],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Pagamentos'
},
(int) 2 => object(App\Model\Entity\Pagamento) {
'id' => (int) 70,
'pagamento_situacao_id' => (int) 2,
'recibo_id' => (int) 37,
'valor' => (float) 100,
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [
'pagamento_situacao_id' => true
],
'[original]' => [
'pagamento_situacao_id' => (int) 1
],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Pagamentos'
}
],
'[new]' => false,
'[accessible]' => [
'*' => true
],
'[dirty]' => [],
'[original]' => [],
'[virtual]' => [],
'[errors]' => [],
'[invalid]' => [],
'[repository]' => 'Recibos'
}