douhensheng1131 2014-07-04 06:59
浏览 6
已采纳

Symfony2 FOSuser非唯一电子邮件

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

  • 写回答

1条回答 默认 最新

  • duangan6731 2014-07-04 07:06
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)