Why is my object type in my doctrine2 doctrine not updated when persist () & flush () is called?

That's what I'm doing:

$entity = new Meta(); $obj = new stdClass(); $obj->foo = 15; $obj->bar = 0; $obj->bor = true; $entity->setObject($obj); $em->persist($entity); $em->flush(); $entity = $entityRepository->find(1); var_dump($entity); 

returns:

 object(Jo\Model\Entity)[130] protected 'id' => int 1 protected 'user' => null protected 'object' => object(stdClass)[105] public 'foo' => int 15 public 'bar' => int 0 public 'bor' => boolean true $entity->getObject()->bar = 9; var_dump($entity); 

returns:

 object(Jo\Model\Entity)[130] protected 'id' => int 1 protected 'user' => null protected 'object' => object(stdClass)[105] public 'foo' => int 15 public 'bar' => int 9 public 'bor' => boolean true 

!

  $em->persist($entity); $em->flush(); 

But after cleaning, the object is not updated in the database.

Perhaps this is because I am setting a new object from the getObject () method, and it has something to do with links or so, but I donโ€™t understand why the second var_dump () shows the correct values โ€‹โ€‹to the object.

The recipient is quite simple and simply consists in returning private property.

Any ideas I would like to understand this behavior.

+4
source share
1 answer

I was not sure about this behavior, but I was curious, so I briefly talked about this in the IRC with Jonathan Wage, one of the developers of Doctrine.

According to John, the reason for this behavior is that when matching an object with an object, like you, you must clone the object โ€” in other words, you need a new instance.

When checking to see if your data has changed, D2 checks to see if the object is the same as the old one. Basically this is just a comparison of x === y, so if you do not have a completely new instance, it will not recognize the data as changed.

+4
source

All Articles