简体   繁体   English

如何覆盖Symfony2包中的实体

[英]How to overwrite an Entity in Symfony2 bundle

I'm using FOSUserBundle for Symfony2 , and I need to be able to register a user without validating if the email is Unique , I just need a valid email so many users can have the same email ( I know this is weird, but I need it). 我正在使用FOSUserBundle for Symfony2 ,我需要能够注册用户而不验证电子邮件是否是唯一的 ,我只需要一个有效的电子邮件,这样很多用户就可以拥有相同的电子邮件 (我知道这很奇怪,但我需要它)。

I have an entity "User" in a bundle that extends FOSUserBundle , is it possible to overwrite the column definition of emailCanonical to eliminate the unique parameter and remove the validation from the FormType? 我在扩展FOSUserBundle的包中有一个实体“User”,是否可以覆盖 emailCanonical 的列定义以消除unique参数并从FormType中删除验证?

I'm using Annotation for mapping my Entities and YML for validation of my forms. 我正在使用Annotation来映射我的实体和YML以验证我的表单。

I had a similar issue few weeks ago. 几周前我遇到过类似的问题 The only solution is to extend model instead of entity: 唯一的解决方案是扩展模型而不是实体:

use FOS\UserBundle\Model\User as BaseUser;

class User extends BaseUser

The downside is that you have to also copy everything from FOS User entity to your User entity. 缺点是您还必须复制从FOS用户实体到用户实体的所有内容。 On the other hand, you can adjust the functionality to your needs. 另一方面,您可以根据需要调整功能。

I found something, it's partially a work around , not viable solution. 我找到了一些东西,这部分是一种解决方法 ,而不是可行的解决方案。

The problem is divided in two parts: the form and the model. 问题分为两部分:形式和模型。

For the form: To overwrite the unique constraint of FOSUserBundle Registration Form you can modify the registration validations groups in the app/config/config.yml: 对于表单:要覆盖FOSUserBundle注册表单的唯一约束,您可以修改app / config / config.yml中的注册验证组:

fos_user:
  registration:
    validation_groups: [YourValidationGroup]

For the model: I haven't found a good solution without overwriting all the model. 对于模型:我没有找到一个好的解决方案,而没有覆盖所有的模型。 Doctrine2 explicitly tells that you can't overwrite a property of a Mapped Class. Doctrine2明确告诉您不能覆盖Mapped类的属性。 So I did a migration that drop the unique constraint index with DoctrineMigrationBundle... I'm not proud of this solution but it works and it let me save a duplicate email. 所以我做了一个使用DoctrineMigrationBundle删除唯一约束索引的迁移...我不为这个解决方案感到自豪,但它有效,它让我保存了一个重复的电子邮件。

If you have better solution for the model don't hesitate! 如果您有更好的解决方案,请不要犹豫!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM