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 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?