多对一关联映射到 Doctrine 2 中的类表继承实体

ManyToOne association mapping to a Class Table Inheritance entity in Doctrine 2(多对一关联映射到 Doctrine 2 中的类表继承实体)
本文介绍了多对一关联映射到 Doctrine 2 中的类表继承实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Author 实体,它是一个包含 AuthorUser 和一个 AuthorGroup 的类表继承.

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

/**
 * Author
 *
 * @ORMTable
 * @ORMEntity
 * @ORMInheritanceType("JOINED")
 * @ORMDiscriminatorColumn(name="type", type="string")
 * @ORMDiscriminatorMap({"user" = "AuthorUser", "group" = "AuthorGroup"})
 */
class Author {
    // ...
}

AuthorUser 与我的 User 实体相关,AuthorGroup 与我的 Group 实体相关.

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

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

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

我不知道如何反转这个.无论如何,问题是我必须将此 CTI 添加到我的 Article 实体字段中.我如何使用 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
     *
     * @ORMManyToOne(targetEntity="Author", inversedBy="?????????")
     * @ORMJoinColumn(name="author_id", referencedColumnName="id")
     */
    protected $author;
}

我不确定如何使其尽可能透明.当我创建一个新的 Article 时,我需要向 author 字段提供一个 UserGroup 对象.我遵循了这种行为,但似乎没有帮助.它变得更加复杂.

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.

这篇关于多对一关联映射到 Doctrine 2 中的类表继承实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)