我正在使用 Doctrine2 来管理我的模型,如下:有一个抽象概念Content
与复合模式Gallery
,也是一个抽象概念Media
从中Video
and Image
继承。
我的选择是添加鉴别器Content
and Media
表以便区分Gallery
, Video
and Image
. Content
uses JOIN inheritance
and Media
uses 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
这是我的类和注释:
内容.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;
}
媒体.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;
}
图库.php
/** @Entity */
class Gallery extends Content
{
/** @OneToMany(targetEntity="Content", mappedBy="container") */
private $contents;
}
视频.php
/** @Entity */
class Video extends Media
{
/** @Column(type="integer") */
private $bitrate;
/** @Column(type="integer") */
private $duration;
}
图片.php
/** @Entity */
class Image extends Media
{
}
我问:这是正确的行为吗?不应该Media
只有字段id
, width
and height
, plus bitrate
and duration
from Video
?
另外,有没有办法去掉不必要的东西?Gallery
table?
我希望我说得足够清楚,不过,请随意提问。先感谢您。
UPDATE:决不。我试图找到一个更简单的例子,但没有显示出这种行为。
有什么建议么?这可能是 Doctrine 2 中的一个错误,还是我缺少一个更简单的解决方案?