我正在使用FOSuserBundle。 我从两天开始搜索如何允许用户收到可供其他人使用的电子邮件。 我想允许它,因为有些人有一个共享的电子邮件地址,但我没有找到我必须覆盖的捆绑文件。
当我尝试使用现有的电子邮件地址注册新用户时,Symfony会给我一个SQL异常:“完整性约束违规”。 在FOSuser模型中,没有此约束的注释...
答案 0 :(得分:4)
您必须覆盖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" />
基本上
不推荐使用unique =“true”
属性来满足您的要求,我希望您知道如何覆盖此文件..
现在我要覆盖一些提示:
首先,你将按照上面给出的文件 https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.md
和您将覆盖
的子实体FOS \ UserBundle \ Model \ User class
应采用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 app / console doctrine:generate:entity
它还将在
中生成'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”,它将根据要求覆盖。
现在只需进行架构更新,您的代码肯定会正常运行,因为我已经完成了它并且它工作得很好..