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).

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?

I'm using Annotation for mapping my Entities and YML for validation of my forms.


I had a similar issue few weeks ago. The only solution is to extend model instead of entity:

use FOSUserBundleModelUser as BaseUser;

class User extends BaseUser

The downside is that you have to also copy everything from FOS User entity to your User entity. 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:

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. 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.

If you have better solution for the model don't hesitate!

链接地址: http://www.djcxy.com/p/54852.html

上一篇: 为什么COM对象不使用IDisposable?

下一篇: 如何覆盖Symfony2包中的实体