2014-06-21 90 views
0

我需要这样定义的媒体实体:继承与协会

类别:

  • 名:字符串(25)
  • 毛坯:字符串(25)
  • 说明:文字
  • 文件:与文件相关联

Document = @ORM \ InheritanceType( “SINGLE_TABLE”):

  • 类别:协会(类别)和鉴别器
  • 标题:字符串(50)
  • 描述:文本
  • 芯子:字符串(50)

例如,类:
VIDEO:

  • 的YouTube
  • 位DailyMotion
  • VIMEO

PHOTO:

  • 现场照片
  • 云照片

FILE:

  • 区域文件
  • 云文件

类别的文档将文档\的Youtube等的实例...

是否有可能使用列作为关联领域和鉴别器继承?

回答

0

official documentation似乎没有提到对此的限制。

但是,只要你想在列的关系,鉴别地图会再靠DB-发出的ID,这是不是在我看来不错...

但你仍然可以让学说处理鉴别为你:只声明你永远不会直接使用的鉴别器列,声明其地图,然后依靠实体逻辑来保持类别和文档类型之间的一致性:

/** 
* @ORM\Entity(repositoryClass="... 
* @ORM\InheritanceType("SINGLE_TABLE") 
* @ORM\DiscriminatorColumn(name="type", type="string") 
* @ORM\DiscriminatorMap({ 
*  "youtube"="Youtube", 
*  "dailymotion"="Dailymotion", 
*  ... 
* }) 
*/ 
abstract class Document { 

    /** 
    * @ORM\ManyToOne(targetEntity="Category", inversedBy="documents") 
    */ 
    protected $category; 
    ... 
    //this is to be called by children that all share a category field 
    public function setCategory(Category $c) { 
     $class = get_class($this); 
     if (strtolower($c->getName()) !== strtolower(substr($class, strrpos($class, '\\') + 1))) { 
      throw new \LogicException("Cannot bind " . $class . " to category " . $c->getName()); 
     } 
     $this->category = $c; 
     return $this; 
    }