How to update doctrine object type field

Tracking policy of Doctrine checks whether or not an object has changed, in your case - User class. However, when checking if $this->properties has changed all Doctrine does is check if it still points to the same object in memory (!).

If you want to force it to update stored object, you can either copy all it's properties to a new object instance (new UserProperties and then reassign it to $this->properties), clone it, or change Doctrine tracking policy to NOTIFY (see Doctrine docs).

Last one however, will require you to change all setters of the object and actually implement notification mechanism (as shown in Doctrine docs) so when this issue popped up in my code, I just recreated the object stored (which is my first suggestion, as it's simple).

I do think however, that this is kind of unexpected behaviour, so I'd open a ticket/issue in Doctrine issue tracker, so that at least a documentation warns about this.


Problem here is that Doctrine does not check if the values of the serialised object has changed, it only checks if the object is the same one (same memory pointer).

If you are working with object serialiser in Doctrine you need to always create a new instance of the object to save it.

I.e.

$user = $form->getData();

$properties = new UserProperties();
$properties->setProperty1($oldValue1);
$properties->setProperty2($newValue2);
...
$user->setProperties($properties);

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

Of course you should be able to create a copy of the object and assign only the changed values.

$properties = clone $user->getProperties();
$properties->setProperty1($newValue);

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