2014-06-21 91 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; 
    }