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 UsuarioBundleEntity;

use FOSUserBundleModelUser as BaseUser;
use DoctrineORMMapping as ORM;
use GedmoMappingAnnotation as Gedmo;
use GedmoTimestampableTraitsTimestampableEntity;
use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;
use DoctrineCommonCollectionsArrayCollection;
use MisdPhoneNumberBundleValidatorConstraintsPhoneNumber as AssertPhoneNumber;

/**
 * @ORMEntity
 * @ORMTable(name="usuarios_externos.usuarios", schema="usuarios_externos")
 * @ORMInheritanceType("JOINED")
 * @ORMDiscriminatorColumn(name="discr", type="string")
 * @ORMDiscriminatorMap({
 *     "natural" = "Natural",
 *     "empresa" = "Empresa"
 * })
 * @UniqueEntity(fields={"correo_alternativo"}, message="El correo electrónico ya está siendo usado, por favor introduzca otro.")
 * @GedmoSoftDeleteable(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 "UsuarioBundleEntityUsuario" sub class of "FOSUserBundleModelUser". 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 ComunBundleModel;

use DoctrineORMMapping as ORM;

trait IdentifierAutogeneratedEntityTrait {

    /**
     * @ORMId
     * @ORMColumn(type="integer", nullable=false, unique=true)
     * @ORMGeneratedValue(strategy="SEQUENCE")
     */
    protected $id;

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

}

Now I include the trait on Usuario entity:

use ComunBundleModelIdentifierAutogeneratedEntityTrait;

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:

[SymfonyComponentDebugExceptionContextErrorException] Runtime Notice: FOSUserBundleModelUser and ComunBundleModelId
entifierAutogeneratedEntityTrait define the same property ($id) in the comp osition of UsuarioBundleEntityUsuario. 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?


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="FOSUserBundleModelUser">

    <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.

/**
 * @ORMId
 * @ORMColumn(type="integer")
 * @ORMGeneratedValue(strategy="AUTO")
 */
protected $id;

FOSUserBundleModelUser is a "Storage agnostic user object". Override id in your entity with more detailed definition:

/**
 * @var integer $id
 *
 * @ORMColumn(name="id", type="integer")
 * @ORMId
 * @ORMGeneratedValue(strategy="AUTO")
 */
protected  $id;
链接地址: http://www.djcxy.com/p/69384.html

上一篇: FOSUserBundle表单扩展

下一篇: 在尝试从FOSUserBundle中的BaseUser进行扩展时发生MappingException