nbonniot nbonniot - 5 months ago 46
PHP Question

FOS User override unique constraint on user storage

I built a multisite app, with following requirements : user should be able to create accounts on differents sites of the collection, with same email and username.

Here is what I did :


  1. Creating UserBundle with FOSUserBundle as parent

  2. Add AttributeOverride on username, usernameCanonical, email, and emailCanonical fields.

  3. Disable initial validation groups (registration and profile) by overriding validation groups.



Point 2:

/**
* User
* The user class to work with, based on FOSUser one.
* To improve loose coupling, don't forget to link FOS\UserBundle\Model\UserInterface instead.
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="LCH\UserBundle\Entity\UserRepository")
*
* @ORM\AttributeOverrides({
* @ORM\AttributeOverride(
* name="email",
* column=@ORM\Column(
* type="string",
* name="email",
* length=255,
* nullable=false,
* unique=false
* )
* ),
* @ORM\AttributeOverride(
* name="emailCanonical",
* column=@ORM\Column(
* type="string",
* name="email_canonical",
* length=255,
* nullable=false,
* unique=false
* )
* ),
* @ORM\AttributeOverride(
* name="username",
* column=@ORM\Column(
* type="string",
* name="username",
* length=255,
* nullable=false,
* unique=false
* )
* ),
* @ORM\AttributeOverride(
* name="usernameCanonical",
* column=@ORM\Column(
* type="string",
* name="username_canonical",
* length=255,
* nullable=false,
* unique=false
* )
* )
* })
*/


Point 3

fos_user:
profile:
form:
validation_groups: [Default]
registration:
form:
validation_groups: [Default]


Last 2 points achieved thanks to this question.

But on form validation, I still have error triggered telling me that email address is already used.

My last attempt was trying to override vendor/friendsofsymfony/user-bundle/Resources/config/storage-validation/orm.xml, (where unique constraint lies) by redefining it in my child bundle. It didn't work either, and this was just a "desperate" try, because if it turned out working, I wouldn't have understood why using preceeding points.

Answer

I finally found the problem, which was almost as usual between chair and screen.

config.yml BEFORE

registration:
        form:
            type: LCH\UserBundle\Form\RegistrationType

    profile:
        form:
            validation_groups:  [Default] # Here you can also add your own groups if you have extra validation
    registration:
        form:
            validation_groups:  [Default] # Here you can also add your own groups if you have extra validation

The registration.form key was defined twice... After merging them...

profile:
        form:
            validation_groups:  [Default]
    registration:
        form:
            validation_groups:  [Default]
            type: LCH\UserBundle\Form\RegistrationType

...Everything rocks.

Comments