使用Doctrine2时的多重歧视

使用Doctrine2时的多重歧视

本文介绍了使用Doctrine2时的多重歧视的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用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时的多重歧视的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 10:16