FOSUserBundle:删除emailCanonical的唯一索引

时间:2012-01-28 06:06:23

标签: symfony fosuserbundle

我正在尝试删除emailCanonical上的唯一索引,以便多个用户可以共享相同的电子邮件地址。但是,我不想直接编辑FOS / UserBundle / Resources / config / doctrine / User.orm.xml,因为对bundle本身的任何更新都将删除更改。有没有什么办法可以覆盖我自己的bundle中的emailCanonical字段,同时扩展基本用户(FOS / UserBundle / Model / User.php)

use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Foo\BarBundle\Constant\SecurityConstant;

class User extends BaseUser {
    protected $id;
...
}

提前致谢!

3 个答案:

答案 0 :(得分:39)

现在标记为(2014年10月14日)的答案根本不是正确答案。

这是唯一正确的解决方案:

namespace XXX\UserBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
 * @ORM\Entity
 * @ORM\Table(name="User_User")
 * @ORM\Entity(repositoryClass="UserRepository")
 * @UniqueEntity(fields="usernameCanonical", errorPath="username", message="fos_user.username.already_used")
 * @ORM\AttributeOverrides({
 *      @ORM\AttributeOverride(name="email", column=@ORM\Column(type="string", name="email", length=255, unique=false, nullable=true)),
 *      @ORM\AttributeOverride(name="emailCanonical", column=@ORM\Column(type="string", name="email_canonical", length=255, unique=false, nullable=true))
 * })
 */
class User extends BaseUser
{
...
}

您还需要覆盖用户表单的验证组:

# app/config/config.yml
...
fos_user:
profile:
    form:
        validation_groups:  [Default] # Here you can also add your own groups if you have extra validation
registration:
    form:
        validation_groups:  [Default] # Here you can also add your own groups if you have extra validation

我们做了什么?我们只是将验证组覆盖为与FOS默认验证不匹配。相反,您的表单只会在Default组中进行验证。上面描述的没有任何组的验证UniqueEntity将由Default组匹配。

答案 1 :(得分:7)

执行此操作的唯一方法是扩展FOS\UserBundle\Model\User类,然后自己重​​新执行所有映射(User.orm.xml中的所有内容)。

来源:

答案 2 :(得分:1)

Tim回答的补充,这就是你使用YAML的方式:

My\UserBundle\Entity\User:
    type: entity
    table: null
    repositoryClass: My\UserBundle\Entity\UserRepository
    attributeOverride:
      usernameCanonical:
        unique: false
        name: usernameCanonical
        column: username_canonical
        length: 255
        nullable: false
        type: string
      emailCanonical:
        unique: false
        name: emailCanonical
        column: email_canonical
        length: 255
        nullable: false
        type: string
    fields:
        id:
          type: integer
          id: true
          generator:
              strategy: AUTO
        firstName:
          type: string
          length: 255