使用FOSUserBundle与基于yml的实体有关 [英] Use FOSUserBundle in relation with yml-based Entities

查看:103
本文介绍了使用FOSUserBundle与基于yml的实体有关的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从头开始,我开始了一个Symfony2项目,然后我安装了 FOSUserBundle

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

然后,我写了(实际上, ORM Designer)一些需要与他们之间以及与User实体有关系的实体。

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.

由于我使用FOSUserBundle我只有一个基本的用户类( 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.

但是,在这一点上,尝试加载浏览器之前登录的URL(当我只安装了 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'
为'X\MyBundle\Entity\User'。

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

以下操作(例如生成CRUD逻辑)将不起作用,只要我在<$ c中有一个 *。orm.ym l文件$ c> config / doctrine 文件夹。如果我删除那些,CRUD生成将工作,但生成实际的mysql表将不会。

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.

玩这些让我到一个点,我也可以得到表但是,如果我尝试使用新生成的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的实体与基于FOSU​​serBundle的用户实体关系,并且仍然能够获得不错的命令行生成工具?

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只显示你如何使用注释,所以这里是一个快速转换为yaml格式,当你的X\MyBundle\Entity\User扩展FOSUSerBundle的UserEntity:

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" }

剩下的东西由FOSUserBundle ,因为BaseModel是一个映射超类,已经描述 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.

这篇关于使用FOSUserBundle与基于yml的实体有关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆