伸出我限定从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
,并且不能使用特质?
你可以看看我的主版本? – ReynierPM 2014-09-23 16:06:11