2014-03-04 59 views
0

假設我有一個公司,我管理Employees, Cars, Contracts, Buildings, Sites, Products,等。你可以猜到,這些是非常獨立的事情,所以沒有繼承是可能的。鏈接1實體與許多其他類實體

對於每一個元素(即實體)的,我希望能夠將一個或幾個Document S(點擊一個按鈕,窗體打開,選擇一個/多個Document或上傳一個新的)。

Document鏈接到一種實體不是問題,我的問題是有很多種實體。我應該如何管理?我有2個想法有自己的問題...:

  1. 創建文檔和員工,另一個文件和汽車之間的一個多對多的關係,等等。
    • 問題:我要重複控制器添加文檔的代碼,複製表單等。
  2. 創建包含文檔ID,相關實體的ID和相關實體的類名的單個連接表。
    • 問題:它看起來不太乾淨,我沒有真正挖掘這種方式,但我覺得我會遇到很多「實體映射」問題。

有什麼建議嗎?


[編輯]

其實我要做的Event也是相同的:我需要一些Events鏈接到一些Employees和/或一些Cars等在我真正的情況下,我有超過10個實體鏈接到Event和/或Document,這意味着如果我使用解決方案1,則複製更多20倍的代碼!

+0

怎麼樣一個單向的ManyToMany?文檔是否關心在您的域中鏈接到哪些實體? –

+0

單向是好的,但問題依然存在:即使如此,我也不得不復製表格/治療或製作奇怪的常見表格/治療。 – Blacksad

回答

0

所以最後我設法解決我的問題,以下@ Rpg600想法有關映射超。

這可能不是最好的和最乾淨的解決方案,我並不是很自豪,但它的確做到了,而且比我的第一個想法還要好。

我創建了一個BaseEntity這是我的一個映射超類(Employee,Car等實體必須擴展這個類):因爲它是不可能有一個超多到多關係

/** 
* BaseEntity 
* @ORM\MappedSuperclass 
*/ 
class BaseEntity { 

    /** 
    * @ORM\OneToOne(targetEntity="MyProject\MediaBundle\Entity\Folder") 
    */ 
    private $folder; 


    /** 
    * Set folder 
    * @param \Webobs\MediaBundle\Entity\Folder $folder 
    * @return BaseEntity 
    */ 
    public function setFolder(\Webobs\MediaBundle\Entity\Folder $folder = null){ 
     $this->folder = $folder;   
     return $this; 
    } 

    /** 
    * Get folder 
    * @return \Webobs\MediaBundle\Entity\Folder 
    */ 
    public function getFolder(){ 
     return $this->folder; 
    }   
} 

,我用一個Folder其中將包含一個或幾個Document。這是解決方案中不好的部分;在folder表基本上只包含一個字段,它是id ...

class Folder 
{ 
    private $id; 

    /** 
    * Note : Proprietary side 
    * @ORM\ManyToMany(targetEntity="MyProject\MediaBundle\Entity\Document", inversedBy="folders", cascade={"persist"}) 
    * @ORM\JoinTable(name="document_in_folder") 
    */ 
    private $documents; 
    // setters and getters 

然後,我創建了一個輔助類(它被聲明爲一個服務)來管理任何EntityDocument之間的聯繫:

class DocumentHelper extends Controller 
{ 
    protected $container; 

    /** ************************************************************************ 
    * Constructor 
    * @param type $container 
    **************************************************************************/ 
    public function __construct($container = null) 
    { 
     $this->container = $container; 
    } 


    /** ************************************************************************ 
    * Attach Document(s) to an $entity according to the information given in the 
    * form. 
    * @param Entity $entity 
    * @param string $redirectRouteName Name of the route for the redirection after successfull atachment 
    * @param string $redirectParameters Parameters for the redirect route 
    * @return Response 
    **************************************************************************/ 
    public function attachToEntity($entity, $redirectRouteName, $redirectParameters) 
    { 
     $folder = $entity->getFolder(); 
     if($folder == NULL){ 
      $folder = new Folder(); 
      $entity->setFolder($folder); 
     } 

     $form = $this->createForm(new FolderType(), $folder); 

     // ------------- Request Management ------------------------------------ 
     $request = $this->get('request'); 
     if ($request->getMethod() == 'POST') { 
      $form->bind($request); // Link Request and Form 

      if ($form->isValid()) { 
      $em = $this->getDoctrine()->getManager(); 
      $em->persist($folder); 
      $em->persist($entity); 
      $em->flush(); 

      return $this->redirect($this->generateUrl($redirectRouteName, $redirectParameters)); 
      } 
     } 


     return $this->render('MyProjectMediaBundle:Folder:addDocument.html.twig', array(
      'form'  => $form->createView(), 
      'entity' => $entity, 
     )); 
    } 

做這樣一來,我只需要在每個相關的控制器添加一個小的動作,讓我們說EmployeeController.php

public function addDocumentAction(Employee $employee) 
    {   
     $redirectRouteName = 'MyProjectCore_Employee_see'; 
     $redirectParameters = array('employee_id' => $employee->getId()); 
     return $this->get('myprojectmedia.documenthelper')->attachToEntity($employee,$redirectRouteName,$redirectParameters); 
    } 

相同的顯示原理,在助手中我具有通用功能,我在我已有的seeAction()中調用並在TWIG文件中導入常用的「文檔列表」顯示。

這就是所有人!

我希望這可以幫助:)

1

假設你使用Doctrine ORM,我想你正在尋找Mapped Superclasses的繼承。

的文檔是勝於言:

http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html#mapped-superclasses

+0

這是interresting謝謝,我不知道那些Mapped超級類。但是,似乎在Mappedsuper類中不可能有多對多(或甚至是一對多)關係。我將繼續對這些貼圖超類進行調查,以瞭解我是否可以找到解決方法;如果您有任何建議,請不要猶豫。無論如何,這對我的項目的其他部分會很有用:) – Blacksad

相關問題