开发者

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 FOSUserB开发者_C百科undle, 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 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. 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!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜