I have entity User
which has many to many relation to roles. I've tried to implement Optimistic lock, everything works fine, just when I changed roles, it doesn't change the version (User entity version), is this proper behaviour?
class User {
/**
* User's roles.
*
* @ORM\ManyToMany(targetEntity="Role")
*/
private $roles;
...
Doctrine 2's locking mechanisms don't take associations into account. They only protect against changes to the entities themselves. IMHO this is expected, because it has no way to know which associations to include and which to ignore. This isn't something you'd want to happen blindly on all associations.
Theoretically Doctrine 2 could achieve this by adding an option to the association mappings, but at this moment that simply isn't supported.
So you have 2 options: