SideWinder
SideWinder

Reputation: 141

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.

Upvotes: 14

Views: 15519

Answers (7)

Nafaa Azaiez
Nafaa Azaiez

Reputation: 131

I know this is an old post, but i found a solution, at least it worked for me, maybe it would be helpful for someone else.

Well, i had the same issue and i'm using a custom manager for the fos_user, in the declaration file config.yml under doctrine entity_manager custom manager i declared the mapping to FOS_userBundle, BUT what was missing is to tell FOS_user that we use a diffrent manager and that's by adding :

fos_user:
---- db_driver: orm
---- model_manager_name: MyCustom_Manager

Upvotes: 0

Bitclaw
Bitclaw

Reputation: 831

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

Upvotes: 13

Yassine EL MALKI
Yassine EL MALKI

Reputation: 162

Your class that extends BaseUser should be like this:

<?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;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
 * User
 * @ORM\Entity
 * @ORM\Table(name="user")
 * @UniqueEntity(fields="email", message="your costum error message")
 *
 */
class myUser extends BaseUser
{
    /**
     * @ORM\Id()
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
}

?>

Upvotes: 0

vince
vince

Reputation: 1

Got the same bug just now, and I solved it. Doctrine throws this Mappingexception in ClassMetadataInfo when it cannot find the related property (attribute or relation) in its mapping.

So, in order to override "emailCanonical" attribute, you need to use "attribute-overrides" and "attribute-override" (as you did), and redefine php class property in your entity :

<?php
...
class YourUser extends BaseUser
{
    ...
    /**
     * Email canonical
     *
     * @var string
     *
     * @ORM\Column(name="email_canonical", type="string", length=255, unique=false)
     */
    protected $emailCanonical;

@EDIT : using this solution causes me another bug.

It solved the one about "Invalid field override named…", but I got another one with "Duplicate definition of column…" when trying to validate schema with php app/console doctrine:schema:validate command.

Any idea ?

Upvotes: 0

Alexey B.
Alexey B.

Reputation: 12033

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.

Upvotes: 3

Pavel Murzakov
Pavel Murzakov

Reputation: 11

It seems that FOSUserBundle entities aren't imported correctly into your project. Make sure FOSUserBundle is present in "mappings" section ("doctrine" branch) of your config.yml

doctrine:
    orm:
        entity_managers:
            default:
                connection:       default
                mappings:
                    AcmeDemoBundle: ~
                    FOSUserBundle: ~

Upvotes: 1

Adam Elsodaney
Adam Elsodaney

Reputation: 7808

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>

Upvotes: 3

Related Questions