ManyToOne关联映射到Doctrine中的Class Table继承实体2 [英] ManyToOne association mapping to a Class Table Inheritance entity in Doctrine 2

查看:111
本文介绍了ManyToOne关联映射到Doctrine中的Class Table继承实体2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个作者实体,它是一个Class Table继承,包含一个 AuthorUser 和一个 AuthorGroup

I have an Author entity, which is a Class Table Inheritance containing an AuthorUser and an AuthorGroup.

/**
 * Author
 *
 * @ORM\Table
 * @ORM\Entity
 * @ORM\InheritanceType("JOINED")
 * @ORM\DiscriminatorColumn(name="type", type="string")
 * @ORM\DiscriminatorMap({"user" = "AuthorUser", "group" = "AuthorGroup"})
 */
class Author {
    // ...
}

AuthorUser 与我的用户实体和 AuthorGroup 到我的实体。

AuthorUser relates to my User entity and AuthorGroup to my Group entity.

class AuthorUser extends Author
{
    /**
     * @var User
     *
     * @ORM\ManyToOne(targetEntity="User", inversedBy="?????")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     */
    protected $user;
}

class AuthorGroup extends Author
{
    /**
     * @var Group
     *
     * @ORM\ManyToOne(targetEntity="Group", inversedBy="?????")
     * @ORM\JoinColumn(name="group_id", referencedColumnName="id")
     */
    protected $user;
}

我不知道如何反转这个。无论如何,问题是我必须将这个CTI添加到我的文章实体字段中。如何将ManyToOne与本文实体字段相关联?

I have no idea how to inverse this. Anyway, the problem is that i have to add this CTI to my Article entity field. How can i relate using ManyToOne to this Article entity field?

class Article
{
    /**
     * @var Author
     *
     * @ORM\ManyToOne(targetEntity="Author", inversedBy="?????????")
     * @ORM\JoinColumn(name="author_id", referencedColumnName="id")
     */
    protected $author;
}

我不知道如何使它尽可能透明。当我创建一个新的文章时,我需要提供用户对象到作者字段。我遵循这个行为,但似乎没有帮助。它变得更加复杂

I'm not sure how to make this as transparent as possible. When i create a new Article, i need to provide either an User or Group object to the author field. I followed this behavior, but it doesn't seem to help. It gets even more complicated.

推荐答案

一个解决方案可能是始终有AuthorGroups,即使只有一个作者。

One solution could be to always have AuthorGroups, even when there's only one Author.

否则,请查看 https://github.com/FabienPennequin/DoctrineExtensions-Rateable

您可能可以使用该代码提供类似的Authored界面,可以区分AuthorUser和AuthorGroup。

You might be able to use that code to provide a similar Authored interface that can discriminate between the AuthorUser and AuthorGroup.

这篇关于ManyToOne关联映射到Doctrine中的Class Table继承实体2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆