FriendsOfSymfony / FOSUserBundle

Provides user management for your Symfony project. Compatible with Doctrine ORM & ODM, and custom storages.
https://symfony.com/doc/master/bundles/FOSUserBundle/index.html
MIT License
3.25k stars 1.57k forks source link

Replacing the mapping #1154

Open laherreria opened 11 years ago

laherreria commented 11 years ago

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?

leopro commented 11 years ago

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

b-durand commented 11 years ago

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

rat4m3n commented 11 years ago

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)?

vishalmelmatti commented 10 years ago

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

pawmart commented 10 years ago

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

Zyostes commented 9 years ago

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

binhle410 commented 8 years ago

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

Garinn commented 6 years ago

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