问题
I have a user entity, and multiple profile entities. What I'm trying to do is dynamically create a oneToOne relationship based on the user role.
So picture my 3 tables:
users (core table, username, pass, role etc, used for authentication)
users_admin
users_client
Then in my User.orm.yml I have:
oneToOne:
--profile:
----targetEntity: \UserBundle\Entity\Profile
----mappedBy: user
The problem is I need the targetEntity to either be:
AdminProfile
ClientProfile
Based on role. Is there any links or advice? I'm completely lost at where to start so appreciate any input on how I might achieve this.
回答1:
Sounds like you want some kind of inheritance.
The idea being that you have an (abstract) "Profile" entity, with concrete subclasses "UserProfile" and "AdminProfile".
Then your user has a oneToOne relationship with a "Profile", which can be of either type.
来源:https://stackoverflow.com/questions/6719290/doctrine2-symfony2-onetoone-with-multiple-entities