Optimistic lock in Doctrine2 doesn't work for many to many relation

1.4k views Asked by At

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;
...
2

There are 2 answers

2
Jasper N. Brouwer On BEST ANSWER

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:

  1. Try to implement such a feature, and submit a PR :)
  2. Implement your own optimistic locking mechanism that does take this specific association into account.
16
Karol Wojciechowski On

I didn't try, but I think this is proper bahaviour (because flush don't modify User entity) and there is no reason to lock User entity - it's not changed.