I like most people are trying to override the FOSUserBundle roles so I can map them ManyToMany to a Role Entity.
Unfortunately for some reason due to the mapping of the Model/User I get the following:
Property "roles" in "Acme\DemoBundle\Entity\User" was already declared, but it must be declared only once
There seems to be some workaround mentioned in this git issue posted in FOSUserBundle:
https://github.com/FriendsOfSymfony/FOSUserBundle/pull/1081#issuecomment-19027818
I am Doctrine ORM and using Annotations for mapping not yml or xml. Latest Symfony (2.4) and latest FOSUB.
I tried the alternative option by copying everything into my Entity and not extending, but to be honest that messed up everything.
I am trying to attempt the idea of creating my own Model/User extending FOSUserBundle/Model/User with no mappings. And then extend my Entity/User from this. I tried but I still got the same issue. I'm assuming I did this incorrectly.
Can someone advise/show how I would do this correctly?
I really need to be able to override roles as although the FOSUserBundle is great, the adaptation of roles isn't very good. Although I appreciate at the time this was the only way they could do it and changing it now breaks BC.
Hope someone can help.
Kind regards Paul Pounder
I had the same Issue, using Annotations also.
Note: As some readers had issues putting all together I created a gitHub repo with my UserBundle. If you find that there's something missing in this HowTo, let me know so I add it.
This post covers three aspects, DB based Roles with Tree structure implementation, framework configuration to also support RoleHierarchy (getReachableRoles) for DB roles. Without which it'd be useless to have roles in DB after all. And Doctrine Subscribers to create Roles upon certain Entity being persisted.
The changes FOS had to make were deep, and well documented, but I must say that a single
HowTo Use
sample code would have prevented me from reading a lot, (not complaining, at least I know a little on compiler passes now.)Roles to DB
I'm using Sf 2.4, but this should work since 2.3. Here is my solution's involved files, consider one step per file:
In the
copmoser.json
I upgraded doctrine-bundle so it includes required files:In the
Bundle.php
file you must register a compiler passThis is the dependency imported by the new version of doctrine-bundle:
I assume that this mapping info is added later than FOSUSerBundle's because I just repeated the process (simplified for only ORM) I saw in FOSUerBundle.php hoping it'd take precedence and it did.
The mappings in
User.orm.xml
are the exact copy of./vendor/friendsofsymfony/user-bundle/FOS/UserBundle/Resources/config/doctrine/model/User.orm.xml
with line #35 commented out. This deletes the conflicting mapping on roles in the mapped super class.From now on, you just do what you wanted to do in 1st place, implement your idea of Roles. Here's mine: The User Class that extends FOS\UserBundle\Model\User, but now with the mappings you used in your bundle.
src/Application/UsuarioBundle/Entity/Role.php
And the Role Class:
src/Application/UsuarioBundle/Entity/Usuario.php
After doing it you- can see that the right SQL changes are dumped by schema update --dump-sql.
Still, I'm not representing Role hierarchies, which I need.
Hope it's useful for someone. I'm sure you already solved this, or lost your job :p.
Documentation I followed:
RoleHierarchy Implementation
Files involved in the Solution:
you can see files in this gist
Or access each file direcctly, (as Gist doesn't preserve the listing order).
src/Application/UsuarioBundle/Role/RoleHierarchy.php
src/Application/UsuarioBundle/Resources/config/services.xml
src/Application/UsuarioBundle/Entity/Role.php
src/Application/UsuarioBundle/Entity/Usuario.php
app/config/security.yml
src/Application/UsuarioBundle/Controller/RoleController.php
src/Application/UsuarioBundle/Form/RoleType.php
src/Application/UsuarioBundle/Resources/views/Role/edit.html.twig
Documentation followed:
cookbook: security voters
Components: Security
reference: Service tags priorities
Souce: RoleHierarchyInterface
Doctrine Subscribers
You'll get this far to realize there's something missing...
The main reason I ported Roles to DB is because I'm dealing with a dynamic (from a Structural perspective) Application that enables the user to configure a workflow. When I add a new Area, with a new Process, with a new Activity (or update either the name or the parent-Child relationship, or remove any), I need new Roles to be generated
automatically
.Then you think of the Doctrine Subscribers for LyfeCycleEvents, but adding new entities in the PrePersist/PreUpdate will demand a nested flush, which in my case messes things up, its easier when you just need to update some fields on already "computedChanges" entities.
So what I used to hook and create/edit/delete roles is the onFlush, at which point the computChangeSet() works fine for adding new entities..
I'll leave the ProcessRolesSubscriber Gist as an example.