FOSUserBundle icon indicating copy to clipboard operation
FOSUserBundle copied to clipboard

Replacing the mapping

Open laherreria opened this issue 11 years ago • 8 comments

I need to replace the mapping of the user entity because I need to use a legacy database. So I created a User class which extends FOS\UserBundle\Model\User and declares its own mapping (using annotations) as explained in "Replacing the mapping of the bundle" https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/doctrine.md

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Symfony\Component\Security\Core\User\EquatableInterface;
use Serializable;


/**
 * @ORM\Entity(repositoryClass="UserRepository")
 * @ORM\Table
 */
class User extends BaseUser implements AdvancedUserInterface, EquatableInterface, Serializable {
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(name="login", type="string", length=50)
     */
    protected $login;

    /**
     * @ORM\Column(name="paswd", type="string", length=100)
     */
    protected $password;


    // ....
}

But it seems that Doctrine sees both mappings and it throws this error:

Fatal error: Uncaught exception 'Doctrine\ORM\Mapping\MappingException' with message 'Property "password" in "MPG\AuthenticacionBundle\Entity\User" was already declared, but it must be declared only once' in /var/www/mpg/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/MappingException.php:238

Do I need to add something else for this to work?

laherreria avatar Jun 10 '13 10:06 laherreria

I think is related with https://github.com/FriendsOfSymfony/FOSUserBundle/pull/1081#issuecomment-19027818

leopro avatar Jun 10 '13 10:06 leopro

@laherreria the right solution is in comments of #982 (ndlr use attribute override)

b-durand avatar Jun 12 '13 20:06 b-durand

This is pretty annoying. Is this a problem with Doctrine changing mapping rules and introducing AttributeOverride and AssociationOverride or this is FOSUserBundle.

I have the same problem trying to override 'roles' as I handle this via ManyToMany and can not use AssociationOverride as there is no such syntax to define ManyToMany there.

/**
 * @ORM\ManyToMany(targetEntity="Role", indexBy="name", cascade={"persist"}, fetch="EXTRA_LAZY")
 * @ORM\JoinTable(name="app_users_roles")
 */
protected $roles;

Shouldn't we have a TAG with version available for the old approach? (allowing overrides)?

rat4m3n avatar Sep 01 '13 13:09 rat4m3n

@rat4m3n you got any solution to this? I am trying to do same thing

vishalmelmatti avatar Aug 21 '14 11:08 vishalmelmatti

My solution to this was not to inherit anything and define my User object without extending FOSModel at all.

pawmart avatar Aug 21 '14 18:08 pawmart

I am in the same situation. Did you find a solution ? (an other rather than inheriting anything). Thank you.

Zyostes avatar Dec 11 '14 08:12 Zyostes

Why don't we refactor FOSUserBundle to use the EasyExtends feature from Sonata-project ? https://sonata-project.org/bundles/easy-extends/master/doc/reference/why.html

binhle410 avatar May 07 '16 01:05 binhle410

I found this to be a far easier solution that also works with MongoDB:

http://blog.lavoie.sl/2015/01/symfony-override-doctrine-mapping.html

Garinn avatar Apr 19 '18 12:04 Garinn