如何扩展FOSUserBundle的User实体?
我有我的 UserBundle,它扩展了 FOSUserBundle 并且工作正常。但现在,我想创建具有不同属性的多个用户实体。 但问题是,当我创建用户实体来扩展我的主用户实体时,如下所示:
class User extends BaseUser
{
protected $id;
// The main user class who extends FOSUser entity
}
class UserB extends User
{
//
}
当我这样做时,出现错误:`
必须保护对 MyApp\UserBundle\Entity\UserB::$id 的访问级别(如在类 MyApp\UserBundle\Entity\User 中)。
当我在 UserB 实体中创建受保护的 id 时,我有以下内容:
PHP 致命错误:无法重新声明 MyApp\UserBundle\Entity\UserB::$id。
最后,我无法删除用户实体中的 id,除非返回 Doctrine 错误:
[Doctrine\ORM\Mapping\MappingException]
没有为实体“MTS\UserBundle\Entity\User”指定标识符/主键。每个实体都必须有一个标识符/主键。
有人可以帮助我吗?
编辑:问题解决了。我的代码:
/**
* MTS\UserBundle\Entity\User
*
* @ORM\Entity
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="type", type="string")
* @ORM\DiscriminatorMap({"userfb" = "UserFB"})
*/
abstract class User extends BaseUser
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string $type
*/
private $type;
}
/**
* @ORM\Table()
* @ORM\Entity()
*/
class UserB extends User
{
// My variables
}
I have my UserBundle who extends the FOSUserBundle and works fine. But now, I want create multiple users entities with différents properties.
But the problem is when I create my user enttity who extends my main User entity like this :
class User extends BaseUser
{
protected $id;
// The main user class who extends FOSUser entity
}
class UserB extends User
{
//
}
When I do this, I've got an error : `
Access level to MyApp\UserBundle\Entity\UserB::$id must be protected (as in class MyApp\UserBundle\Entity\User).
And when I create a protected id in my UserB entity I have this:
PHP Fatal error: Cannot redeclare MyApp\UserBundle\Entity\UserB::$id.
And to finish, I can't delete the id in my User entity less return a Doctrine error :
[Doctrine\ORM\Mapping\MappingException]
No identifier/primary key specified for Entity 'MTS\UserBundle\Entity\User'. Every Entity must have an identifier/primary key.
Someone can help me ?
EDIT : Problem solved. My code :
/**
* MTS\UserBundle\Entity\User
*
* @ORM\Entity
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="type", type="string")
* @ORM\DiscriminatorMap({"userfb" = "UserFB"})
*/
abstract class User extends BaseUser
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string $type
*/
private $type;
}
/**
* @ORM\Table()
* @ORM\Entity()
*/
class UserB extends User
{
// My variables
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的问题似乎是缺少注释。
我能够通过从我的工作代码中删除此错误消息来复制您的“每个实体必须有一个标识符/主键”错误消息:
对我有用:
You problem appears to be the lack of annotation.
I was able to replicate your 'Every Entity must have an identifier/primary key' error message by removing this from my working code:
The works for me: