沃梦达 / 编程问答 / php问题 / 正文

多对一关联映射到 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 中的类表继承实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:多对一关联映射到 Doctrine 2 中的类表继承实体

基础教程推荐