问题描述
我正在使用Doctrine2来管理我的模型:在 Gallery
内容 >,也是一个抽象概念媒体
从视频
和图像
继承。
I'm using Doctrine2 to manage my model below: There's an abstract concept Content
with a Composite pattern in Gallery
, also an abstract concept Media
from which Video
and Image
inherits.
我的选择是将歧视者添加到内容
和媒体
表格,以区分画廊
,视频
和图像
。 内容
使用 JOIN继承
和媒体
使用 SINGLE_TABLE继承
。
My choice was to add discriminators to Content
and Media
tables in order to differentiate between Gallery
, Video
and Image
. Content
uses JOIN inheritance
and Media
uses SINGLE_TABLE inheritance
.
当我运行 doctrine orm:schema-tool:create --dump-sql
,媒体
表是从内容
中复制列。这是命令的输出:
As I run doctrine orm:schema-tool:create --dump-sql
, Media
table is duplicating columns from the Content
one. That's the output of the command:
CREATE TABLE Content (id INT AUTO_INCREMENT NOT NULL, container_id INT DEFAULT NULL, creationDate DATETIME NOT NULL, publicationDate DATETIME DEFAULT NULL, isGallery TINYINT(1) NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB;
CREATE TABLE Media (id INT AUTO_INCREMENT NOT NULL, creationDate DATETIME NOT NULL, publicationDate DATETIME DEFAULT NULL, width INT NOT NULL, height INT NOT NULL, isImage TINYINT(1) NOT NULL, bitrate INT NOT NULL, duration INT NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB;
CREATE TABLE Gallery (id INT AUTO_INCREMENT NOT NULL, PRIMARY KEY(id)) ENGINE = InnoDB;
ALTER TABLE Content ADD FOREIGN KEY (container_id) REFERENCES Gallery(id);
ALTER TABLE Gallery ADD FOREIGN KEY (id) REFERENCES Content(id) ON DELETE CASCADE
以下是我的课程和注释:
Here are my classes and annotations:
Content.php
Content.php
/** @Entity
* @InheritanceType("JOINED")
* @DiscriminatorColumn(name="isGallery", type="boolean")
* @DiscriminatorMap({
* 0 = "Media",
* 1 = "Gallery"
* })
*/
abstract class Content
{
/** @Id @GeneratedValue @Column(type="integer") */
private $id;
/** @Column(type="datetime") */
private $creationDate;
/** @Column(type="datetime", nullable="true") */
private $publicationDate;
/** @ManyToOne(targetEntity="Gallery", inversedBy="contents") */
private $container;
}
Media.php
Media.php
/** @Entity
* @InheritanceType("SINGLE_TABLE")
* @DiscriminatorColumn(name="isImage", type="boolean")
* @DiscriminatorMap({
* 0 = "Video",
* 1 = "Image"
* })
*/
abstract class Media extends Content
{
/** @Column(type="integer") */
private $width;
/** @Column(type="integer") */
private $height;
}
Gallery.php
Gallery.php
/** @Entity */
class Gallery extends Content
{
/** @OneToMany(targetEntity="Content", mappedBy="container") */
private $contents;
}
Video.php
Video.php
/** @Entity */
class Video extends Media
{
/** @Column(type="integer") */
private $bitrate;
/** @Column(type="integer") */
private $duration;
}
Image.php
Image.php
/** @Entity */
class Image extends Media
{
}
我问:这是正确的行为吗?不应该媒体
只有字段 id
, width
和高度
,加上 bitrate
和持续时间
从视频
?
I ask: That's the correct behaviour? Should not Media
have only the fields id
, width
and height
, plus bitrate
and duration
from Video
?
此外,有没有办法摆脱不必要的画廊
表?
Besides, is there a way to get rid of the unnecesary Gallery
table?
我希望我说得足够清楚,但是请随时问。谢谢你提前。
I hope I made it clear enough, though, feel free to ask. Thank you in advance.
更新:没有办法。我试图找到一个更简单的例子,没有显示这个行为,但是我没有找到。
UPDATE: No way. I tried to find an even simpler example not showing this behavior, but I found none.
任何建议?这可能是Doctrine 2中的错误,或者我错过了一个更简单的解决方案?
Any suggestions? Could this be a bug in Doctrine 2 or I'm missing a simpler solution?
推荐答案
我自己回答我的问题,希望它会帮助某人有一天。
I answer my question myself hoping it will help someone someday.
我打开了一个这个问题,答案很清楚:不支持。
I opened a bug report in github for doctrine2 with this question and the answer is pretty clear: This is not supported.
更新2013/07/27
我刚刚尝试使用Doctrine 2.3.4它的工作原理。 :D
I just tried this with Doctrine 2.3.4 and it works as expected. :D
这篇关于使用Doctrine2时的多重歧视的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!