简体   繁体   English

原则-OneToOne关系不适用于扩展MappedSuperclass

[英]Doctrine - OneToOne relationship not working with extends MappedSuperclass

I try to use @MappedSuperclass. 我尝试使用@MappedSuperclass。 It works well for simple variable (int, string...) and OneToMany/ManyToOne relationship. 它适用于简单变量(整数,字符串...)和OneToMany / ManyToOne关系。 But OneToOne relationship doesn't work. 但是,一对一关系不起作用。

I have two MappedSuperclass with OneToOne relationship : _SiteUser 我有两个具有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 _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;

And this is my 2 class who extends these MappedSuperclass : SiteUser 这是我的2个班级,他们扩展了这些MappedSuperclass:SiteUser

/**
 * SiteUser
 *
 * @ORM\Entity(repositoryClass="SiteModule\Repository\SiteUserRepository")
 * @ORM\Table(name="site_users")
*/
class SiteUser extends _SiteUser
{
}

SiteUserTo SiteUserTo

/**
 * SiteUserTo
 *
 * @ORM\Entity(repositoryClass="SiteModule\Repository\SiteUserToRepository")
 * @ORM\Table(name="users_to")
*/
class SiteUserTo extends _SiteUserTo
{
}

When I generate entities from my MappedSuperclass (_SiteUser and _SiteUserTo), I got well a table named "users_to" with id and user_id. 当我从我的MappedSuperclass(_SiteUser和_SiteUserTo)生成实体时,我很好地得到了一个名为“ users_to”的表,其ID和user_id。 But when I generate entities from my others classes (SiteUser and SiteUserTo), it creates the table "users_to" with only id field. 但是,当我从其他类(SiteUser和SiteUserTo)生成实体时,它将创建具有唯一ID字段的表“ users_to”。 I don't know why... 我不知道为什么

If I update my SiteUser Class like this : 如果我这样更新我的SiteUser类:

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

And SiteUserTo like this : 和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;

}

I got the same problem, no field user_id in table users_to . 我遇到了同样的问题,表users_to没有字段user_id But the field "test" is well created in the table site_users and in the table users_to ... 但现场“测试”在表中创建良好site_users并在表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. 为了进一步支持继承,必须使用单个或联合表继承功能。

Simply said you can not have @MappedSuperClass and @ORM\\Entity annotations at the same time, hence the unexpected results 简而言之,您不能同时具有@MappedSuperClass和@ORM \\ Entity批注,因此会产生意外的结果

https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/inheritance-mapping.html https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/inheritance-mapping.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM