FOSUserBundle icon indicating copy to clipboard operation
FOSUserBundle copied to clipboard

Cannot override user column types

Open mdopt opened this issue 12 years ago • 4 comments

Hi.

With the current version there is no simple and elegant way to override column types. Let's say i want to use datetimetz instead of datetime.

<?php

namespace Acme\UserBundle\Entity;

use FOS\UserBundle\Model\User as FOSUser;

class User extends FOSUser
{
}

<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping
    xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
    http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd"
>
    <entity name="Acme\UserBundle\Entity\User" table="fos_user">
        <id name="id" type="integer" column="id">
            <generator strategy="AUTO"/>
        </id>

        <attribute-overrides>
            <attribute-override name="lastLogin">
                <field column="last_login" type="datetimetz" nullable="true" />
            </attribute-override>
        </attribute-overrides>
    </entity>
</doctrine-mapping>

After running:

php app/console doctrine:schema:update --force

i get this error:

[Doctrine\ORM\Mapping\MappingException]
The column type of attribute 'lastLogin' on class 'Acme\UserBundle\Entity\U
ser' could not be changed.

Which is not suprising. This behavior is to be expected: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html#attribute-override

The only solution i can think of is not to extend FOS\UserBundle\Model\User and copy/paste all the methods to my own user class:

<?php

namespace Acme\UserBundle\Entity;

use FOS\UserBundle\Model\UserInterface as FOSUserInterface;

class User implements FOSUserInterface
{
    ...
}

which is ugly.

My proposal: remove FOS\UserBundle\Model\User mapping: https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/config/doctrine/model/User.orm.xml

I guess it's related to this: https://github.com/FriendsOfSymfony/FOSUserBundle/commit/6290fd7b6278706327f21aae55844e7d23be0008#commitcomment-3338658

mdopt avatar Sep 30 '13 06:09 mdopt

@mdopt You cannot change the type of the attribute when overriding. Use "datetime" instead of "datetimetz" and it's should work !

nicolassing avatar Oct 07 '13 10:10 nicolassing

@nicolassing Yes, I know you cannot change the attribute type. That's why I created this issue. In the previous version(1.3), there was no mapping for FOS\UserBundle\Model\User, only for FOS\UserBundle\Entity\User: https://github.com/FriendsOfSymfony/FOSUserBundle/blob/1.3.x/Resources/config/doctrine/User.orm.xml so you could extend FOS\UserBundle\Model\User and create your own mapping. In the current version you have to copy-paste FOS\UserBundle\Model\User code to your own user class.

mdopt avatar Oct 08 '13 06:10 mdopt

True ! Clearly the only actual solution is to rewrite the user class and it's not elegant :/

nicolassing avatar Oct 08 '13 09:10 nicolassing

Doctrine does not allow us to override an attribute type. http://doctrine-orm.readthedocs.org/en/latest/reference/inheritance-mapping.html#attribute-override Any other solution for that by using inheritance?

rvanlaak avatar Apr 23 '15 15:04 rvanlaak