Use FOSUserBundle in relation with yml-based Entities

巧了我就是萌 提交于 2019-12-18 16:58:32

问题


I've started a Symfony2 project from scratch where I then installed FOSUserBundle.

Then, I have written (actually, generated with ORM Designer) some entities that need to have relations between them, and with the User entity.

I have Items belonging to Users, Collections belonging to Users that group Items, and so on.

Since I used FOSUserBundle I only have a basic User class (https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.md , step 3a) defined using annotations, no config/doctrine folder and no User.yml file in it.

I then created the MyBundle/Resources/config/doctrine folder and added the yml files mentioned above.

When I try to generate the entities with the command-line tool everything works fine: it will create the Entities from my yml files.

However, at this point, trying to load up in browsers the url where the login previously worked (when I only had the FOSUserBundle installed) will throw this error:

MappingException: No mapping file found named '/var/www/concert/src/X/MyBundle/Resources/config/doctrine/User.orm.yml' for class 'X\MyBundle\Entity\User'.

Following actions, such as generating the CRUD logic, will not work as long as I have an *.orm.yml file in the config/doctrine folder. If I remove those, CRUD generation will work, but generation of actual mysql tables won't.

Juggling with these gets me to a point where I can also get the tables, but then the actual app doesn't work if I try to use any of the url's where the newly generated CRUD is involved because since the entities are based on yml (which I remove to get things "working") it won't have any mapping knowledge.

Is this inherently wrong? To have yml-based entities in relationship with an User entity based on the FOSUserBundle and still be able to get the nice command-line generation tools?


回答1:


The problem you describe stems from mixing configuration formats (yaml and I assume annotations). You can easily fix this by ditching the annotations in your models and replacing them with yaml-files like you would do in your own models.

Unfortunately the FOSUserBundle-docs only show you how to use annotations, so here is a quick transformation into yaml format when your X\MyBundle\Entity\User extends FOSUSerBundle's UserEntity:

X\MyBundle\Entity\User:
    type:  entity
    table: fos_user
    id:
        id:
            type: integer
            strategy: { generator: "AUTO" }

The remaining stuff is taken care of by FOSUserBundle, as the BaseModel is a mapped-superclass and already describes the stuff in the User.orm.xml, but you could just as well replace the existing values or add additional values just like you would do with your own models.

If you don't use annotations throughout your app, you might also want to disable them in your app/config/config.yml to prevent side effects.



来源:https://stackoverflow.com/questions/14984344/use-fosuserbundle-in-relation-with-yml-based-entities

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