使用与基于 yml 的实体相关的 FOSUserBundle [英] Use FOSUserBundle in relation with yml-based Entities
问题描述
我从头开始了一个 Symfony2 项目,然后安装了 FOSUserBundle.
I've started a Symfony2 project from scratch where I then installed FOSUserBundle.
然后,我写了(实际上是用 ORM 设计器生成的)一些需要在它们之间建立关系的实体,以及与用户实体.
Then, I have written (actually, generated with ORM Designer) some entities that need to have relations between them, and with the User entity.
我有 Items
属于 Users
,Collections
属于 Users
那个组 Items代码>,等等.
I have Items
belonging to Users
, Collections
belonging to Users
that group Items
, and so on.
自从我使用 FOSUserBundle 以来,我只有一个基本的 User 类(https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.md ,步骤 3a) 使用注释定义,没有 config/doctrine
文件夹,也没有 <代码>User.yml 文件.
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.
然后我创建了 MyBundle/Resources/config/doctrine
文件夹并添加了上面提到的 yml
文件.
I then created the MyBundle/Resources/config/doctrine
folder and added the yml
files mentioned above.
当我尝试使用命令行工具生成实体时,一切正常:它将从我的 yml 文件创建实体.
When I try to generate the entities with the command-line tool everything works fine: it will create the Entities from my yml files.
但是,此时,尝试在浏览器中加载以前登录的网址(当我只安装了 FOSUserBundle 时)将引发此错误:
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: 未找到命名的映射文件'/var/www/concert/src/X/MyBundle/Resources/config/doctrine/User.orm.yml'对于XMyBundleEntityUser"类.
MappingException: No mapping file found named '/var/www/concert/src/X/MyBundle/Resources/config/doctrine/User.orm.yml' for class 'XMyBundleEntityUser'.
只要我在 config/doctrine
文件夹中有一个 *.orm.ym
l 文件,以下操作(例如生成 CRUD 逻辑)将无法工作.如果我删除这些,CRUD 生成将起作用,但实际 mysql 表的生成将不起作用.
Following actions, such as generating the CRUD logic, will not work as long as I have an *.orm.ym
l file in the config/doctrine
folder. If I remove those, CRUD generation will work, but generation of actual mysql tables won't.
处理这些使我达到了我也可以获得表格的程度,但是如果我尝试使用任何涉及新生成的 CRUD 的 url,那么实际的应用程序将无法工作,因为实体是基于在 yml(我删除它以使事情工作")上,它不会有任何映射知识.
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.
这本质上是错误的吗?将基于 yml 的实体与基于 FOSUserBundle 的 User 实体关联起来,并且仍然能够获得不错的命令行生成工具?
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?
推荐答案
您描述的问题源于混合配置格式(yaml 和我假设注释).您可以通过丢弃模型中的注释并用 yaml 文件替换它们来轻松解决此问题,就像您在自己的模型中所做的那样.
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.
不幸的是,FOSUserBundle-docs 只向您展示了如何使用注释,所以当您的 XMyBundleEntityUser 扩展 FOSUSerBundle 的 UserEntity 时,这里可以快速转换为 yaml 格式:
Unfortunately the FOSUserBundle-docs only show you how to use annotations, so here is a quick transformation into yaml format when your XMyBundleEntityUser extends FOSUSerBundle's UserEntity:
XMyBundleEntityUser:
type: entity
table: fos_user
id:
id:
type: integer
strategy: { generator: "AUTO" }
剩下的东西由 FOSUserBundle 处理,因为 BaseModel 是一个 mapped-superclass 并且已经描述了 User.orm.xml
中的内容,但是您也可以替换现有值或添加其他值,就像您所做的那样您自己的模型.
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.
如果您不在整个应用中使用注释,您可能还想在 app/config/config.yml
中禁用它们以防止副作用.
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.
这篇关于使用与基于 yml 的实体相关的 FOSUserBundle的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!