Symfony2 FOSUserBundle User entity field override

SideWinder picture SideWinder · Mar 21, 2013 · Viewed 15.2k times · Source

I have a problem with overriding an entity. I need the field emailCanonical to be not be unique.

Here is what I've done: In my UserBundle\Resources\config\doctrine\User.orm.xml I've added the following attribute-overrides configuration, according to the Doctrine2 documentation

<attribute-overrides>
    <attribute-override name="emailCanonical">
        <field column="email_canonical" unique="false" name="emailCanonical" />
    </attribute-override>
</attribute-overrides>

Then I ran the following console commands

$ php app/console doctrine:migrations:diff
$ php app/console doctrine:migrations:migrate

Everything worked fine. emailCanonical was made non unique. But now, when I need to generate an entity in other bundles of project, I have a strange error:

 $ php app/console doctrine:generate:entities SkyModelsBundle:Category
 Generating entity "Sky\Bundle\ModelsBundle\Entity\Category"

 [Doctrine\ORM\Mapping\MappingException]
 Invalid field override named 'emailCanonical' for class 'Sky\Bundle\UserBundle\Entity\User'.

 doctrine:generate:entities [--path="..."] [--no-backup] name

However, if I remove the override settings from xml mapping, everything works fine.

Answer

Bitclaw picture Bitclaw · Aug 30, 2014

You override using PHP annotations like so (This is supported starting from doctrine version 2.3 and above, please note that in the documentation it mentions that you cannot override types , however I was able to do so using the latest version of doctrine , at the time of writing it is 2.4.4):

<?php 
namespace Namespace\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use FOS\UserBundle\Model\User as BaseUser;

/**
 * User
 * @ORM\Entity
 * @ORM\AttributeOverrides({
 *     @ORM\AttributeOverride(name="id",
 *          column=@ORM\Column(
 *              name     = "guest_id",
 *              type     = "integer",
 *              length   = 140
 *          )
 *      )
 * })
 */
class myUser extends BaseUser
{
    /**
     * @ORM\Id()
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
}

This is specified in the documentation at: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html#overrides