【问题标题】:Symfony2 FOSuser non-unique emailSymfony2 FOSuser 非唯一电子邮件
【发布时间】:2014-07-04 06:59:56
【问题描述】:

我正在使用 FOSuserBundle。 两天以来,我一直在搜索如何允许用户拥有可以被其他人使用的电子邮件。 我想允许它,因为有些人有一个共享的电子邮件地址,但我没有找到我必须覆盖的捆绑包的哪个文件。

当我尝试使用现有电子邮件地址注册新用户时,Symfony 给了我一个 SQL 异常:“违反完整性约束”。 FOSuser模型中,并没有这个约束的注解...

【问题讨论】:

    标签: php symfony fosuserbundle


    【解决方案1】:

    您必须覆盖 FOSUserBundle\Resources\config\doctrine\model\User.orm.xml,如下所示:

    on line no. 15:  
    
      `<field name="emailCanonical" column="email_canonical" type="string" length="255" unique="true" /> `
    

    应该是

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

    基本上

    唯一 = “真”

    不推荐使用属性来满足您的要求,我希望您知道如何覆盖此文件..

    现在,我在下面给出一些提示:

    首先,您将遵循 https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.md

    以及您将在其中覆盖

    的子实体

    FOS\UserBundle\Model\User 类

    应该是xml格式。

    现在让你的子实体类是

      <?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;
             }
           }
    

    现在,当您通过以下命令以“xml”格式生成上述实体时

    sudo php 应用程序/控制台原则:生成:实体

    它还会在

    中生成一个'User.orm.xml'文件

    Acme\UserBundle\Resources\config\doctrine

    目录。

    现在您将使用一个名为“AttributesOverrides”的属性来覆盖 FOSUserBundle 模型表的基本映射,如下所示

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

    如果你仔细查看“User.orm.xml”文件中的“emailCanonical”,我已经弃用了将根据要求覆盖的属性 unique="true"。

    现在只需进行架构更新,您的代码肯定会工作,因为我已经完成了它并且工作得很好..

    【讨论】:

    • 我在 app/Resources/FOSUSerBundle/config/doctrine/model 中创建了文件,但是当我检查“php app/console dictionary:schema:update --dump-”时,日期库没有变化sql”,并且该文件似乎被忽略了。所以我不知道我的方法是不好还是刚刚好。
    • 您不会在 FOSUserBundle\Resources\config\doctrine\model 中创建“User.orm.xml”,因为“User.orm.xml”已经存在..
    • 我知道,但通常为了覆盖控制器或视图,我们必须在 app 目录中创建相同的文件树,并且将使用该文件。但是对于这种类型的文件,我不知道如何进行,我正在搜索。
    • k,首先将包覆盖为
    猜你喜欢
    • 2015-03-02
    • 2021-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-23
    • 2012-12-24
    • 2019-04-26
    • 2018-07-08
    相关资源
    最近更新 更多