2014-09-23 62 views
0

伸出我限定从FOSUserBundle BaseUser延伸的Usuario实体,我做如下:MappingException尝试从BaseUser在FOSUserBundle

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

    .... 
} 

但我发现了这个错误:

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.

为什么?没有FOS User实体一个id字段定义为PK?

奇怪的问题,而尝试使用性状

添加字段既然我有ID领域的许多表和去重复的代码,并按照良好做法,我决定使用一个特点吧。这是它的外观:

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

} 

现在我包括Usuario实体性状:

use ComunBundle\Model\IdentifierAutogeneratedEntityTrait; 

并尝试在实体类的使用方法:

class Usuario extends BaseUser { 
    use IdentifierAutogeneratedEntityTrait; 

    ... 

} 

得到了同样的错误,为什么没有认识到这个特点?

UPDATE

我跑从Symfony2的shell的命令doctrine:schema:validate,我得到这样的输出:

[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

我需要定义实体的$id,并且不能使用特质?

回答

2

的FOS用户模型具有id属性,但不提供id映射到该属性。您必须重写id属性并提供所需的注释。

//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" /> 

你可以看到,它没有提供id字段的映射信息。你需要将它添加到你的实体。

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

你可以看看我的主版本? – ReynierPM 2014-09-23 16:06:11

2

FOS\UserBundle\Model\User是一个“存储不可知用户对象”。在实体覆盖id有更详细的定义:

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

你能看看我的主版本吗? – ReynierPM 2014-09-23 18:30:21