Doctrine2, Symfony2 - oneToOne with multiple entities?

…衆ロ難τιáo~ 提交于 2019-12-24 18:57:58

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!