ManyToOne association mapping to a Class Table Inheritance entity in 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
字段提供一个 User
或 Group
对象.我遵循了这种行为,但似乎没有帮助.它变得更加复杂.
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 中的类表继承实体
基础教程推荐
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01