2014-03-07 49 views
0

我從未與symfony/doctrine一起工作過,並且正在努力做到這一點。我創建了一個ProjectBundle和一個FeatureBundle使用oneToMany組織symfony和doctrine配置

我有以下接口和類我ProjectBundleEntity目錄:

Project.php (implements ProjectInterface) 
ProjectInterface.php 
ProjectItemInterface.php 

在我的應用程序,一個Project可以有很多Features但因爲我試圖完全不同的擔憂,ProjectBundle沒有按不需要知道FeatureBundle。我沒有任何ProjectItemInterface的實現,因爲我設想它有許多不同的實現。

我計劃爲我的應用程序提供一個CoreBundle,將所有捆綁在一起。我試圖分開關注,如Sylius正在做。

我很困惑我應該如何在每個包中的原則配置。以下是我對Project.orm.ymlProjectBundle

Acme\Bundle\ProjectBundle\Entity\Project: 
    type: mappedSuperclass 
    id: 
     id: 
      type: integer 
      generator: 
       strategy: AUTO 
    fields: 
     name: 
      type: string 
      length: 255 
     createdAt: 
      type: datetime 
      gedmo: 
       timestampable: 
        on: create 
     updatedAt: 
      type: datetime 
      gedmo: 
       timestampable: 
        on: update 
      nullable: true 
    oneToMany: 
     items: 
      targetEntity: Acme\Bundle\ProjectBundle\Entity\ProjectItemInterface 
      mappedBy: project 
      orphan-removal: true 
      cascade: 
       - all 

我很困惑,雖然因爲ProjectItemInterface是多態的。在我的應用程序的情況下,它需要是Feature,但我可能需要在將來添加一些其他實體類型,如Task或某物。

ProjectInterface具有通用的::addItem(ProjectItemInterface $item)::getItems()定義的方法。

這些方法在ProjectBundle中有意義,但不在CoreBundle中。看來我想要的方法,如::addFeature(FeatureInterface $task)::addTask(TaskInterface $task)。這是一個混亂的領域。

另一個是Project.orm.yml文件應該看起來像在CoreBundle中,因爲我將擴展該基礎Project實體並添加特定於應用程序的特性和方法。

我已經閱讀了關於mapped superclasses,我相信我會擴展基本Project時使用,但我不知道如何配置在CoreBundleoneToManyProjectItemInterface關係。我已經讀了一些關於鑑別器映射的內容,但是我認爲這對於可以相互擴展的類來說。我的ProjectItemInterface實現不會擴展基地ProjectItem

要回顧一下:

  1. 請問我捆/實體結構是否合理?
  2. 我的ProjectBundle是否過於一般,我應該在我的CoreBundle中包含項目內容並完成它?
  3. 如果不是,我如何在我的Project中爲我的ProjectItemInterface實現配置多態性oneToMany原則關係?

    /CoreBundle 
     /Entity 
      Project.php (extends ProjectBundle's Project) 
      ProjectInterface.php (extends ProjectBundle's ProjectInterface) 
      Feature.php (extends FeatureBundle's Feature) 
      FeatureInterface.php (extends FeatureBundle's FeatureInterface and ProjectBundle's ProjectItemInterface) 
     /Resources 
      /config 
       /doctrine 
        Project.orm.yml (don't know what this should look like) 
        Feature.orm.yml 

    /ProjectBundle 
     /Entity 
      Project.php 
      ProjectInterface.php 
      ProjectItemInterface.php 
     /Resources 
      /config 
       /doctrine 
        Project.orm.yml 

    /FeatureBundle 
     /Entity 
      Feature.php 
      FeatureInterface.php 
     /Resources 
      /config 
       /doctrine 
        Feature.orm.yml 

回答

0

這是一個相當大的問題。我只想解決幾個問題。

顯然你知道很多關於分離的東西。事實上,你是S2/D2的新手,這讓我想建議你先將Project和Feature捆綁在一起,直到你更熟悉事物的運作方式。我擔心的是你可能會花費太多的精力才能入門。

話雖如此,請通讀這裏:http://symfony.com/doc/current/cookbook/doctrine/resolve_target_entity.html

D2允許您通過可以在您的CoreBundle中定義的接口將您的Project實體與Feature實體關聯。然後您可以在運行時指定實際的Feature類。因此,您的項目永遠不需要知道該功能的來源。它有一些限制,但可能足以滿足您的目的。

第二種方法是完全避免D2關係的東西。相反,當一個項目實體被加載時,它會派發一個請求它的特性的事件。核心事件偵聽器將接收事件,導致查詢並返回特徵。這種方法完全隔離了這兩個捆綁包,但當然可以使得更新等變得更加困難。

希望這至少會給你一些提示,試試哪個方向。

+0

感謝您的回覆!是的,我想我可能只是不幸地在開始時不那麼關注分離。我沒有看到symfony書中的那一章。有趣的,可能是我需要的。儘管如此,我認爲這一章有一個錯誤。在客戶實體註釋中,「在此示例中,InvoiceSubjectInterface中定義的任何方法都已在BaseCustomer中實現」。我認爲這種說法是不正確的。 BaseCustomer在'CustomerBundle'中不應該有發票相關的方​​法,對吧? – David

+0

沒有。在示例中,Customer是InvoiceSubject,因此需要實現任何InvoiceSubject方法。發票完全是一個不同的對象。 – Cerad

+0

正確,但InvoidSubjectInterface方法不會在CustomerBundle的BaseCustomer中實現,如果您想將CustomerBundle從InvoiceBundle中分離出來,它們將在客戶類中實現,該客戶類在CoreBundle中擴展基本客戶。 – David