FOSUserBundle:删除 emailCanonical 的唯一索引

发布于 2024-12-29 08:25:48 字数 432 浏览 1 评论 0原文

我正在尝试删除 emailCanonical 上的唯一索引,以便多个用户可以共享相同的电子邮件地址。但是,我不想直接编辑 FOS/UserBundle/Resources/config/doctrine/User.orm.xml,因为对包本身的任何更新都会删除更改。有什么方法可以覆盖我自己的包中的 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;
...
}

提前致谢!

I am trying to remove the unique index on emailCanonical, so that multiple users can share the same email address. However, I do not want to edit FOS/UserBundle/Resources/config/doctrine/User.orm.xml directly as any updates to the bundle itself will remove the change. Is there any way I can override the emailCanonical field in my own bundle, while extending the base user (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;
...
}

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

浅唱々樱花落 2025-01-05 08:25:48

现在标记的答案(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 组匹配。

The answer that is marked as right now (14 Oct 2014) is not the right answer at all.

This is the only right solution:

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

Also you need to override validation groups for your user form:

# 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

What did we do? We just overrode validation groups to not match the FOS default validations. Instead your form will be validated only with Default group. Described above validation UniqueEntity that doesn't have any group will be matched by Default group.

‖放下 2025-01-05 08:25:48

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

来源:

The only way to do this is to extend the FOS\UserBundle\Model\User class and then re-do all of the mapping (everything in User.orm.xml) yourself.

Sources:

白况 2025-01-05 08:25:48

对 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

Supplement to Tim's answer, this is how you'd do it using 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文