Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Save parent record from child

I have a many to one relation like shown below:

Persona Model.

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

Empleado Model.

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

Suppose I have an $id of Empleado Model.

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

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

```php $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



19.6k

¿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?



19.6k
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.