We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Save parent record from child

I have a many to one relation like shown below:

Persona Model.

 $this->hasMany('PersonaId','Empleado','PersonaId')

Empleado Model.

$this->belongsTo('PersonaId','Persona','PersonaId', array("foreignKey" => true)); 

Suppose I have an $id of Empleado Model.

 $empleado = Empleado::findFirst("EmpleadoId=$id");

I need to update records for both models given that $id so I tried the following:

 $empleado->Persona->PersonaNombres = $this->request->getPost("PersonaNombres","striptags");
 $empleado->Persona->PersonaApellidoPaterno = $this->request->getPost("PersonaApellidoPaterno","striptags");
 $empleado->SitioId = $this->request->getPost("SitioId","int");
 $empleado->TipoEmpleadoId = $this->request->getPost("TipoEmpleadoId","int");
 $empleado->save();

The fields in model Empleado are saved (last two) but the related records are not. How can I save parent related records?

Thanks



33.8k

¿Has probado con $empleado->Persona->save() (aunque dudo que funcione)? Otra cosa podría ser sobreescribir el método save() e implementar la lógica que quieres.

===============================================0

Did you tried $empleado->Persona->save() (but I don't think that will work)? Other way could be overwritting save() method and implement the logic you want.

As you imagined, $empleado->Persona->save() doesn't work. I know I can split the operartion by updating the two models separately, like this:

$empleado = Empleado::findFirst("EmpleadoId={$id}");
$persona = $empleado->Persona;
$persona->PersonaNombres = $this->request->getPost("PersonaNombres","striptags");
$persona->PersonaApellidoPaterno = $this->request->getPost("PersonaApellidoPaterno","striptags");
$empleado->SitioId = $this->request->getPost("SitioId","int");
$empleado->TipoEmpleadoId = $this->request->getPost("TipoEmpleadoId","int");
$persona->save();
$empleado->save();

But I find this inefficient since the related/transaction functionality is lost. Do you have any other idea?

============================================= Efectivamente $empleado->Persona->save() no funciona. Puedo separar la forma de guardar ambos modelos en 2 operaciones diferentes como lo muestro en el código. Sin embargo pierdo las ventajas de guardar datos relacionados en una única transacción, nada me garantiza que ambos modelos se guarden si lo hago por separado. ¿Tienes alguna otra idea?



33.8k
Accepted
answer

¿Y si metes una transacción manual propia? Por lo demás, ni idea, no sé si se podrá.

========

And how about a manual transanction? In the other hand, no idea, I don't know if it'll work.

I ended up with that, thanks.

=================

Al final fue lo que hice, gracias!

¿Y si metes una transacción manual propia? Por lo demás, ni idea, no sé si se podrá.

========

And how about a manual transanction? In the other hand, no idea, I don't know if it'll work.