Deynis
Deynis

Reputation: 327

Symfony2 FOSuser non-unique email

I am using the FOSuserBundle. I search since two days how I can allow user to have an email which can be already used by someone else. I want to allow it because some people haves a shared email adress but I don't found which file of the bundle I have to override.

When I am trying to register a new user with an existing email adresse, Symfony gives me an SQL exception : "Integrity constraint violation". In the FOSuser Model, there is no annotation of this contraint...

Upvotes: 0

Views: 2086

Answers (1)

Ajeet Varma
Ajeet Varma

Reputation: 726

You will have to override the FOSUserBundle\Resources\config\doctrine\model\User.orm.xml as given below :

on line no. 15:  

  `<field name="emailCanonical" column="email_canonical" type="string" length="255" unique="true" /> `

should be as

<field name="emailCanonical" column="email_canonical" type="string" length="255" />

basically

unique = "true"

property is deprecated to make your requirement full fill and i hope you know how to override this file ..

Now for overriding i m giving below some tips :

First u will follow the documentation given on the https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.md

and the child entity in which you will override the

FOS\UserBundle\Model\User class

should be in xml format .

Now lets your child entity class is

  <?php

   namespace Acme\UserBundle\Entity;

   use Doctrine\ORM\Mapping as ORM;
   use FOS\UserBundle\Model\User as AbstractUser;

    /**
     * User
     */
   class User extends AbstractUser
     {
      /**
        * @var integer
        */
      protected $id;

   public function __construct()
     {
      trigger_error(sprintf('%s is deprecated. Extend FOS\UserBundle\Model\User directly.', __CLASS__), E_USER_DEPRECATED);
        parent::__construct();
       }

       /**
         * Get id
         *
         * @return integer 
         */
        public function getId()
        {
          return $this->id;
         }
       }

Now when you generate your above entity in 'xml' format by following command

sudo php app/console doctrine:generate:entity

It will also generate a 'User.orm.xml' file in

Acme\UserBundle\Resources\config\doctrine

directory .

Now you will use a property called "AttributesOverrides" for overriding the basic mapping of FOSUserBundle's model table which is as given below

     <?xml version="1.0" encoding="utf-8"?>
     <doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://doctrine-               project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-     mapping.xsd">

     <entity name="Acme\UserBundle\Entity\User">

     <id name="id" type="integer" column="id">
        <generator strategy="AUTO"/>
     </id>

     <attribute-overrides>

        <attribute-override name="username">
             <field name="username" column="username" type="string" length="255" />
        </attribute-override>

        <attribute-override name="usernameCanonical">
            <field name="usernameCanonical" column="username_canonical" type="string" length="255" unique="true" />
        </attribute-override>

        <attribute-override name="email">
            <field name="email" column="email" type="string" length="255" />
        </attribute-override>

        <attribute-override name="emailCanonical">
            <field name="emailCanonical" column="email_canonical" type="string" length="255"/>
        </attribute-override>

        <attribute-override name="salt">
            <field name="salt" column="salt" type="string" />
        </attribute-override>

        <attribute-override name="password">
            <field name="password" column="password" type="string" />
        </attribute-override>

        <attribute-override name="lastLogin">
            <field name="lastLogin" column="last_login" type="datetime" nullable="true" />
        </attribute-override>

        <attribute-override name="locked">
            <field name="locked" column="locked" type="boolean" />
        </attribute-override>

        <attribute-override name="expired">
            <field name="expired" column="expired" type="boolean" />
        </attribute-override>

        <attribute-override name="expiresAt">
            <field name="expiresAt" column="expires_at" type="datetime" nullable="true" />
        </attribute-override>

        <attribute-override name="confirmationToken">
            <field name="confirmationToken" column="confirmation_token" type="string" nullable="true" />
        </attribute-override>

        <attribute-override name="passwordRequestedAt">
            <field name="passwordRequestedAt" column="password_requested_at" type="datetime" nullable="true" />
        </attribute-override>

        <attribute-override name="passwordRequestedAt">
            <field name="passwordRequestedAt" column="password_requested_at" type="datetime" nullable="true" />
        </attribute-override>

        <attribute-override name="roles">
            <field name="roles" column="roles" type="array" />
        </attribute-override>

        <attribute-override name="credentialsExpired">
            <field name="credentialsExpired" column="credentials_expired" type="boolean" />
        </attribute-override>

        <attribute-override name="credentialsExpireAt">
            <field name="credentialsExpireAt" column="credentials_expire_at" type="datetime" nullable="true" />
        </attribute-override>

        </attribute-overrides>

      </entity>

      </doctrine-mapping>

If u carefully look at "emailCanonical" in "User.orm.xml" file i have deprecated the property unique="true" which will override as per requirement .

Now just do schema update and your code will work surely because i have done it and it is working very fine ..

Upvotes: 4

Related Questions