2014-11-14 41 views
1

我有相關的數據庫表中的以下設置:一對許多數據庫表rellation與Doctrine2

Organization 
+--------+---------+ 
| id | integer | 
+--------+---------+ 
| name | string | 
+--------+---------+ 

Division 
+---------------+---------+ 
|  id  | integer | 
+--------+----------------+ 
|organization_id| integer | 
+---------------+---------+ 
|  name  | string | 
+---------------+---------+ 

Subdivision 
+---------------+---------+ 
|  id  | integer | 
+--------+----------------+ 
| division_id | integer | 
+---------------+---------+ 
|  name  | string | 
+---------------+---------+ 

我使用Symfony2與Doctrine2 ORM和FOSRestBundle

現在我感到困惑與關聯映射。

當我需要的組織,我想獲得以下

{ 
    id: 1, 
    name: "organization1", 
    divisions: [ 
     { 
      id: 1, 
      organization_id: 1, 
      name: "division1" 
      subdivisions: [ 
       { 
        id: 1, 
        division_id: 1, 
        name: "subdivision1" 
       } 
      ] 
     } 
    ] 
} 

我想通了這一個,並用One-To-Many bidirectional association mapping of Doctrine

什麼讓我頭痛,是關係的相反方向。 如果我需要一個specifig細分,我想獲得如下:

{ 
    id: 1, 
    division_id: 1, 
    name: "subdivision1", 
    division: { 
      id: 1, 
      organization_id: 1, 
      name: "division1" 
      organization: { 
        id: 1, 
        name: "organization" 
      } 
     } 
    ] 
} 

但我得到這個:

{ 
    id: 1, 
    division_id: 1, 
    name: "subdivision1", 
    division: { 
     id: 1, 
     organization_id: 1, 
     name: "division1", 
     organization: { 
      id: 1, 
      name: "organization1", 
      divisions: [/* ..list of all the other divisions.. */] 
     }, 
     subdivisions: [/* ..list of all the other subdivisions.. */] 
    } 

}

如何從移除細分和部門使用Doctrine返回數據?

因爲我只需要細分,它所屬的部門和部門所屬的組織。列出所有的樹需要大量的數據和時間,而我並不需要全部。

編輯:

我用下面的檢索記錄(逝去的工作原理espected):

$organization = $this 
     ->getDoctrine() 
     ->getRepository('MyTestBundle:Organization') 
     ->find($id); 

$organization = $this 
     ->getDoctrine() 
     ->getRepository('MyTestBundle:Organization') 
     ->findAll(); 

而向相反的方向:

$subdivision = $this 
     ->getDoctrine() 
     ->getRepository('MyTestBundle:Subdivision') 
     ->find($id); 

這產生以下SQL:

SELECT t0.id AS id1, t0.division_id AS division_id2, t0.name AS name3, t0.division_id AS division_id4 FROM Subdivision t0 WHERE t0.id = ? ["11"] [] 
SELECT t0.id AS id1, t0.organization_id AS organization_id2, t0.name AS name3, t0.organization_id AS organization_id4 FROM Division t0 WHERE t0.id = ? [16] [] 
SELECT t0.id AS id1, t0.name AS name2 FROM Organization t0 WHERE t0.id = ? [15] [] 
SELECT t0.id AS id1, t0.organization_id AS organization_id2, t0.name AS name3, t0.organization_id AS organization_id4 FROM Division t0 WHERE t0.organization_id = ? [15] [] 
SELECT t0.id AS id1, t0.division_id AS division_id2, t0.name AS name3, t0.division_id AS division_id4 FROM Subdivision t0 WHERE t0.division_id = ? [15] [] 
SELECT t0.id AS id1, t0.division_id AS division_id2, t0.name AS name3, t0.division_id AS division_id4 FROM Subdivision t0 WHERE t0.division_id = ? [18] [] 
SELECT t0.id AS id1, t0.division_id AS division_id2, t0.name AS name3, t0.division_id AS division_id4 FROM Subdivision t0 WHERE t0.division_id = ? [19] [] 
SELECT t0.id AS id1, t0.division_id AS division_id2, t0.name AS name3, t0.division_id AS division_id4 FROM Subdivision t0 WHERE t0.division_id = ? [45] [] 
SELECT t0.id AS id1, t0.division_id AS division_id2, t0.name AS name3, t0.division_id AS division_id4 FROM Subdivision t0 WHERE t0.division_id = ? [49] [] 
SELECT t0.id AS id1, t0.division_id AS division_id2, t0.name AS name3, t0.division_id AS division_id4 FROM Subdivision t0 WHERE t0.division_id = ? [51] [] 
SELECT t0.id AS id1, t0.division_id AS division_id2, t0.name AS name3, t0.division_id AS division_id4 FROM Subdivision t0 WHERE t0.division_id = ? [56] [] 
SELECT t0.id AS id1, t0.division_id AS division_id2, t0.name AS name3, t0.division_id AS division_id4 FROM Subdivision t0 WHERE t0.division_id = ? [16] [] 
+2

強烈依賴於您的查詢,因此請向我們展示您當前的查詢的樣子。 – SBH

+0

爲您的請求添加了其他數據 – nn4n4s

回答

0

更換$subdivision ... ->find($id)

$subdivision = $this 
    ->getDoctrine() 
    ->getRepository('MyTestBundle:Subdivision') 
    ->createQueryBuilder('s') 
    ->leftJoin('s.division', 'd') 
    ->leftJoin('d.organization', 'o') 
    ->where('s.id = :id') 
    ->setParameter('id', $id) 
    ->getQuery() 
    ->getSingleResult(); 

這應該已經降低要求的原則做出的計數。當然,organizationdivisions屬性仍然存在,但內容不會自動加載第一個查詢。如果您通過getDivisions()訪問該屬性,則內容將從數據庫中加載。它是Doctrine的一個特性,可以在不讓開發人員考慮的情況下加載必要的數據。當然,它不是最高效的方式,你必須採用這種方式來滿足你的需求。看看lazy loading和相關主題。

+0

是延遲加載和部分對象:http://doctrine-orm.readthedocs.org/en/latest/reference/partial-objects.html – Pradeep