使用Doctrine2时存在多种歧视级别

时间:2010-10-14 11:09:22

标签: php design-patterns doctrine-orm

我正在使用Doctrine2来管理我的模型:在Content中有一个带有复合模式的抽象概念Gallery,也是一个抽象概念MediaVideo并且Image继承。

我的选择是在ContentMedia表格中添加歧视器,以便区分GalleryVideoImageContent使用JOIN inheritanceMedia使用SINGLE_TABLE inheritance

当我运行doctrine orm:schema-tool:create --dump-sql时,Media表正在复制Content个列中的列。这是命令的输出:

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

以下是我的课程和注释:

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;
}

忽略原始

/** @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

/** @Entity */
class Gallery extends Content
{
    /** @OneToMany(targetEntity="Content", mappedBy="container") */
    private $contents;
}

Video.php

/** @Entity */
class Video extends Media
{
    /** @Column(type="integer") */
    private $bitrate;
    /** @Column(type="integer") */
    private $duration;
}

Image.php

/** @Entity */
class Image extends Media
{
}

我问:那是正确的行为吗? Media不应该id只有widthheightbitrate,以及duration的{​​{1}}和Video字段吗?

此外,有没有办法摆脱不必要的Gallery表?

我希望我说得够清楚,但是,请随意问。提前谢谢。

更新:没办法。我试图找到一个更简单的例子,没有显示这种行为,但我没有找到。

有什么建议吗?这可能是Doctrine 2中的错误还是我错过了一个更简单的解决方案?

2 个答案:

答案 0 :(得分:6)

我自己回答我的问题,希望有一天能帮到某人。

我用这个问题打开了一个bug report in github for doctrine2,答案很明确:不支持。

更新2013/07/27

我刚用Doctrine 2.3.4尝试了这个,它按预期工作。 :d

答案 1 :(得分:0)

如果他们都是抽象的,我发现仅将DiscriminatorMap放在顶级实体中会更有意义。