我在下面使用 Doctrine2 来管理我的模型: 有一个Content带有复合模式Gallery的抽象概念,还有一个抽象概念Media,从中继承Video和Image继承。
我的选择是在Content和表中添加鉴别器,Media以区分和。用途和用途。GalleryVideoImageContentJOIN inheritanceMediaSINGLE_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, widthand height, plus bitrateand durationfromVideo吗?
此外,有没有办法摆脱不必要的Gallery表?
我希望我说得足够清楚,不过,请随时提问。先感谢您。
更新:不可能。我试图找到一个更简单的例子,没有显示这种行为,但我没有找到。
有什么建议么?这可能是 Doctrine 2 中的错误还是我缺少更简单的解决方案?