سؤال

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!

هل كانت مفيدة؟

المحلول

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:

نصائح أخرى

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.

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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top