Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 FOSUserBundle User entity field override

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.

like image 615
SideWinder Avatar asked Mar 21 '13 08:03

SideWinder


3 Answers

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

like image 150
Bitclaw Avatar answered Nov 13 '22 23:11

Bitclaw


I believe the name attribute of the field tag is not required, since it is already specified for the attribute-override tag

Try this

<attribute-overrides>
    <attribute-override name="emailCanonical">
        <field column="email_canonical" unique="false" />
    </attribute-override>
</attribute-overrides>
like image 34
Adam Elsodaney Avatar answered Nov 14 '22 00:11

Adam Elsodaney


Quote from official documentation, so may be its the only way.

If you need to change the mapping (for instance to adapt the field names to a legacy database), the only solution is to write the whole mapping again without inheriting the mapping from the mapped superclass.

like image 3
Alexey B. Avatar answered Nov 13 '22 23:11

Alexey B.