Php 使用Doctrine2时的多重区分级别
我正在使用Doctrine2管理下面的模型:有一个抽象概念Php 使用Doctrine2时的多重区分级别,php,design-patterns,doctrine-orm,Php,Design Patterns,Doctrine Orm,我正在使用Doctrine2管理下面的模型:有一个抽象概念内容,在图库中有一个复合模式,还有一个抽象概念媒体,从中继承视频和图像 我的选择是在内容和媒体表中添加鉴别器,以便区分画廊、视频和图像内容使用连接继承和媒体使用单表继承 当我运行orm:schema工具:create--dump sql时,Media表正在从Content表复制列。这是命令的输出: CREATE TABLE Content (id INT AUTO_INCREMENT NOT NULL, container_id INT
内容
,在图库
中有一个复合模式,还有一个抽象概念媒体
,从中继承视频
和图像
我的选择是在内容
和媒体
表中添加鉴别器,以便区分画廊
、视频
和图像
<代码>内容使用连接继承
和媒体
使用单表继承
当我运行orm:schema工具: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;
}
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
/** @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
{
}
我问:这是正确的行为吗?媒体
不应仅包含id
、宽度
和高度
,以及视频
中的比特率
和持续时间
此外,有没有办法摆脱不必要的Gallery
表格
不过,我希望我说得足够清楚,请随意提问。先谢谢你
更新:不行。我试图找到一个更简单的例子,不显示这种行为,但我没有发现
有什么建议吗?这可能是条令2中的一个错误,还是我错过了一个更简单的解决方案?我自己回答我的问题,希望有一天它能帮助别人 我提出了一个问题,答案很清楚:这是不受支持的 更新日期2013/07/27
我刚刚尝试了第2.3.4条,效果如预期:D如果它们都是抽象的,我发现只在顶层实体中放置
DiscriminatorMap
更有意义。我也支持原则2.3.4,但这种行为不起作用。表介质未创建,您有反馈吗?您可以在此处查看我的测试: