简体   繁体   中英

MappingException while trying to extends from BaseUser in FOSUserBundle

I'm defining an Usuario entity that extends from FOSUserBundle BaseUser and I'm do it as follow:

namespace UsuarioBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Timestampable\Traits\TimestampableEntity;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Doctrine\Common\Collections\ArrayCollection;
use Misd\PhoneNumberBundle\Validator\Constraints\PhoneNumber as AssertPhoneNumber;

/**
 * @ORM\Entity
 * @ORM\Table(name="usuarios_externos.usuarios", schema="usuarios_externos")
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="discr", type="string")
 * @ORM\DiscriminatorMap({
 *     "natural" = "Natural",
 *     "empresa" = "Empresa"
 * })
 * @UniqueEntity(fields={"correo_alternativo"}, message="El correo electrónico ya está siendo usado, por favor introduzca otro.")
 * @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false)
 */
class Usuario extends BaseUser {
    /**
     * Hook timestampable behavior
     * updates createdAt, updatedAt fields
     */
    use TimestampableEntity;

    ...

    public function getId()
    {
        return $this->id;
    }

    ....
}

But I'm getting this error:

MappingException: No identifier/primary key specified for Entity "UsuarioBundle\\Entity\\Usuario" sub class of "FOS\\UserBundle\\Model\\User". Every Entity must have an identifier/primary key.

Why? Has not FOS User entity a id field defined as PK?

Weird issue while try to add field using Traits

Since I'll have the ID field in many tables and for de-duplicate code and follow good practices I decide to use a Trait for it. This is how it looks:

namespace ComunBundle\Model;

use Doctrine\ORM\Mapping as ORM;

trait IdentifierAutogeneratedEntityTrait {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer", nullable=false, unique=true)
     * @ORM\GeneratedValue(strategy="SEQUENCE")
     */
    protected $id;

    public function getId()
    {
        return $this->id;
    }

}

Now I include the trait on Usuario entity:

use ComunBundle\Model\IdentifierAutogeneratedEntityTrait;

And try to use in entity class:

class Usuario extends BaseUser {
    use IdentifierAutogeneratedEntityTrait;

    ...

}

Got the same error, why is not recognizing the trait?

UPDATE

I ran the command doctrine:schema:validate from Symfony2 shell and I get this output:

[Symfony\\Component\\Debug\\Exception\\ContextErrorException] Runtime Notice: FOS\\UserBundle\\Model\\User and ComunBundle\\Model\\Id
entifierAutogeneratedEntityTrait define the same property ($id) in the comp osition of UsuarioBundle\\Entity\\Usuario. This might be incompatibl e, to improve maintainability consider using accessor methods in traits ins tead. Class was composed in /var/www/html/sencamer/src/UsuarioBund
le/Entity/Usuario.php line 500

Do I need to define the $id in the entity and can not use a Trait?

FOS\\UserBundle\\Model\\User is a "Storage agnostic user object". Override id in your entity with more detailed definition:

/**
 * @var integer $id
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected  $id;

The FOS User model has the id property, but is does not provide the id mapping to that property. You have to override the id property and supply the required annotation.

//excerpt from the FOSUserbundle doctrine mapping config
<mapped-superclass name="FOS\UserBundle\Model\User">

    <field name="username" column="username" type="string" length="255" />

    <field name="usernameCanonical" column="username_canonical" type="string" length="255" unique="true" />

You can see, it does not provide mapping info for the id field. You need to add it to your entity.

/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM