我有相關的數據庫表中的以下設置:一對許多數據庫表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] []
強烈依賴於您的查詢,因此請向我們展示您當前的查詢的樣子。 – SBH
爲您的請求添加了其他數據 – nn4n4s