Doctrine - OneToOne 关系不适用于扩展 MappedSuperclass
Doctrine - OneToOne relationship not working with extends MappedSuperclass
我尝试使用@MappedSuperclass。它适用于简单变量(int、字符串...)和 OneToMany/ManyToOne 关系。但是 OneToOne 关系不起作用。
我有两个具有 OneToOne 关系的 MappedSuperclass:
_SiteUser
/**
* @MappedSuperclass _SiteUser
*
* @ORM\Entity(repositoryClass="_SiteModule\_Repository\_SiteUserRepository")
* @ORM\Table(name="site_users")
*/
class _SiteUser
{
/**
* @var int
* @Groups("id")
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
* @Groups({"username"})
*
* @ORM\Column(name="username", type="string", length=255, unique=true)
*/
protected $username;
/**
* @var string
* @Groups({"password"})
*
* @ORM\Column(name="password", type="string", length=255)
*/
protected $password;
/**
* @var _SiteUserTo
*
* @ORM\OneToOne(targetEntity="_SiteModule\_Entity\_SiteUserTo", mappedBy="user")
* @Gedmo\Versioned()
*/
protected $user_to;
_SiteUserTo
/**
* @MappedSuperclass _SiteUserTo
*
* @ORM\Entity(repositoryClass="_SiteModule\_Repository\_SiteUserToRepository")
* @ORM\Table(name="users_to")
*/
class _SiteUserTo
{
/**
* @var int
* @Groups("id")
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var _SiteUser
*
* @ORM\OneToOne(targetEntity="_SiteModule\_Entity\_SiteUser", inversedBy="user_to")
* @JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;
这是我的 2 class 扩展了这些 MappedSuperclass :
站点用户
/**
* SiteUser
*
* @ORM\Entity(repositoryClass="SiteModule\Repository\SiteUserRepository")
* @ORM\Table(name="site_users")
*/
class SiteUser extends _SiteUser
{
}
SiteUserTo
/**
* SiteUserTo
*
* @ORM\Entity(repositoryClass="SiteModule\Repository\SiteUserToRepository")
* @ORM\Table(name="users_to")
*/
class SiteUserTo extends _SiteUserTo
{
}
当我从我的 MappedSuperclass(_SiteUser 和 _SiteUserTo)生成实体时,我得到了一个名为 "users_to" 的 table,id 和 user_id。但是当我从其他 classes(SiteUser 和 SiteUserTo)生成实体时,它会创建只有 id 字段的 table "users_to"。我不知道为什么...
如果我像这样更新我的 SiteUser Class :
/**
* Class SiteUser
*
* @ORM\Entity(repositoryClass="SiteModule\Repository\SiteUserRepository")
* @ORM\Table(name="site_users")
*/
class SiteUser extends _SiteUser
{
/**
* @var boolean
*
* @ORM\Column(name="test", type="boolean")
*/
protected $test;
/**
* @var SiteUserTo
*
* @ORM\OneToOne(targetEntity="SiteModule\Entity\SiteUserTo", mappedBy="user")
*/
protected $user_to;
}
SiteUserTo 点赞:
/**
* Class SiteUserTo
*
* @ORM\Entity(repositoryClass="SiteModule\Repository\SiteUserToRepository")
* @ORM\Table(name="users_to")
*/
class SiteUserTo extends _SiteUserTo
{
/**
* @var boolean
*
* @ORM\Column(name="test", type="boolean")
*/
protected $test;
/**
* @var SiteUser
*
* @ORM\OneToOne(targetEntity="SiteModule\Entity\SiteUser", inversedBy="user_to")
* @JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;
}
我遇到了同样的问题,table users_to
中没有字段 user_id
。但是字段 "test" 在 table site_users
和 table users_to
...
中创建得很好
A mapped superclass cannot be an entity, it is not query-able and
persistent relationships defined by a mapped superclass must be
unidirectional (with an owning side only). This means that One-To-Many
associations are not possible on a mapped superclass at all.
Furthermore Many-To-Many associations are only possible if the mapped
superclass is only used in exactly one entity at the moment. For
further support of inheritance, the single or joined table inheritance
features have to be used.
简单地说你不能同时有@MappedSuperClass和@ORM\Entity注解,因此出现意想不到的结果
https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/inheritance-mapping.html
我尝试使用@MappedSuperclass。它适用于简单变量(int、字符串...)和 OneToMany/ManyToOne 关系。但是 OneToOne 关系不起作用。
我有两个具有 OneToOne 关系的 MappedSuperclass: _SiteUser
/**
* @MappedSuperclass _SiteUser
*
* @ORM\Entity(repositoryClass="_SiteModule\_Repository\_SiteUserRepository")
* @ORM\Table(name="site_users")
*/
class _SiteUser
{
/**
* @var int
* @Groups("id")
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var string
* @Groups({"username"})
*
* @ORM\Column(name="username", type="string", length=255, unique=true)
*/
protected $username;
/**
* @var string
* @Groups({"password"})
*
* @ORM\Column(name="password", type="string", length=255)
*/
protected $password;
/**
* @var _SiteUserTo
*
* @ORM\OneToOne(targetEntity="_SiteModule\_Entity\_SiteUserTo", mappedBy="user")
* @Gedmo\Versioned()
*/
protected $user_to;
_SiteUserTo
/**
* @MappedSuperclass _SiteUserTo
*
* @ORM\Entity(repositoryClass="_SiteModule\_Repository\_SiteUserToRepository")
* @ORM\Table(name="users_to")
*/
class _SiteUserTo
{
/**
* @var int
* @Groups("id")
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @var _SiteUser
*
* @ORM\OneToOne(targetEntity="_SiteModule\_Entity\_SiteUser", inversedBy="user_to")
* @JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;
这是我的 2 class 扩展了这些 MappedSuperclass : 站点用户
/**
* SiteUser
*
* @ORM\Entity(repositoryClass="SiteModule\Repository\SiteUserRepository")
* @ORM\Table(name="site_users")
*/
class SiteUser extends _SiteUser
{
}
SiteUserTo
/**
* SiteUserTo
*
* @ORM\Entity(repositoryClass="SiteModule\Repository\SiteUserToRepository")
* @ORM\Table(name="users_to")
*/
class SiteUserTo extends _SiteUserTo
{
}
当我从我的 MappedSuperclass(_SiteUser 和 _SiteUserTo)生成实体时,我得到了一个名为 "users_to" 的 table,id 和 user_id。但是当我从其他 classes(SiteUser 和 SiteUserTo)生成实体时,它会创建只有 id 字段的 table "users_to"。我不知道为什么...
如果我像这样更新我的 SiteUser Class :
/**
* Class SiteUser
*
* @ORM\Entity(repositoryClass="SiteModule\Repository\SiteUserRepository")
* @ORM\Table(name="site_users")
*/
class SiteUser extends _SiteUser
{
/**
* @var boolean
*
* @ORM\Column(name="test", type="boolean")
*/
protected $test;
/**
* @var SiteUserTo
*
* @ORM\OneToOne(targetEntity="SiteModule\Entity\SiteUserTo", mappedBy="user")
*/
protected $user_to;
}
SiteUserTo 点赞:
/**
* Class SiteUserTo
*
* @ORM\Entity(repositoryClass="SiteModule\Repository\SiteUserToRepository")
* @ORM\Table(name="users_to")
*/
class SiteUserTo extends _SiteUserTo
{
/**
* @var boolean
*
* @ORM\Column(name="test", type="boolean")
*/
protected $test;
/**
* @var SiteUser
*
* @ORM\OneToOne(targetEntity="SiteModule\Entity\SiteUser", inversedBy="user_to")
* @JoinColumn(name="user_id", referencedColumnName="id")
*/
protected $user;
}
我遇到了同样的问题,table users_to
中没有字段 user_id
。但是字段 "test" 在 table site_users
和 table users_to
...
A mapped superclass cannot be an entity, it is not query-able and persistent relationships defined by a mapped superclass must be unidirectional (with an owning side only). This means that One-To-Many associations are not possible on a mapped superclass at all. Furthermore Many-To-Many associations are only possible if the mapped superclass is only used in exactly one entity at the moment. For further support of inheritance, the single or joined table inheritance features have to be used.
简单地说你不能同时有@MappedSuperClass和@ORM\Entity注解,因此出现意想不到的结果
https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/inheritance-mapping.html