Use FOSUserBundle in relation with yml-based Entit

2019-02-10 09:00发布

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条回答
一夜七次
2楼-- · 2019-02-10 09:37

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.

查看更多
登录 后发表回答