我目前遇到了Doctrine Hydration的一些問題。我使用ZF2來構建一個API,因此我需要構建一些利用連接的查詢。Doctrine Hydrate將實體加入到一個陣列條目中
現在他們有點像這樣:
$qb->select(array('r, a'))
->from('Release\Entity\Release', 'r')
->leftJoin(
'Artist\Entity\Artist', a',
\Doctrine\ORM\Query\Expr\Join::WITH, 'a.id = r.artist'
)
->orderBy('r.id', 'ASC');
所以,如果我滋潤結果與AbstractQuery::HYDRATE_ARRAY
的結果將是爲每個連接表/實體額外的數組項。
Array
(
[0] => Array
(
[id] => 1
[artist] => 1
[title] => A super nice Release
[digital_release] => DateTime Object
(
[date] => 2014-01-25 00:00:00.000000
[timezone_type] => 3
[timezone] => Europe/Berlin
)
[physical_release] => DateTime Object
(
[date] => 2014-01-25 00:00:00.000000
[timezone_type] => 3
[timezone] => Europe/Berlin
)
)
[1] => Array
(
[id] => 1
[first_name] => John
[last_name] => Doe
[artist_name] => JD and the Beat-Machines
[created_at] => DateTime Object
(
[date] => 2015-04-17 13:16:18.000000
[timezone_type] => 3
[timezone] => Europe/Berlin
)
[updated_at] => DateTime Object
(
[date] => 2015-04-17 13:16:18.000000
[timezone_type] => 3
[timezone] => Europe/Berlin
)
)
[2] => Array
(
[id] => 2
[artist] => 14
[title] => Some other nice album
[digital_release] => DateTime Object
(
[date] => 2014-02-01 00:00:00.000000
[timezone_type] => 3
[timezone] => Europe/Berlin
)
[physical_release] => DateTime Object
(
[date] => 2014-02-01 00:00:00.000000
[timezone_type] => 3
[timezone] => Europe/Berlin
)
)
[...]
)
正如您所見,相應版本[0]的藝術家存儲在array-key [1]中。
如果我最終打印結果爲新的JsonModel($result)
這對我來說似乎並不可行。
我想要實現的是這樣的
Array
(
[0] => Array
(
[id] => 1
[artist] => Array
(
[id] => 1
[first_name] => John
[last_name] => Doe
[artist_name] => JD and the Beat-Machines
[created_at] => DateTime Object
(
[date] => 2015-04-17 13:16:18.000000
[timezone_type] => 3
[timezone] => Europe/Berlin
)
[updated_at] => DateTime Object
(
[date] => 2015-04-17 13:16:18.000000
[timezone_type] => 3
[timezone] => Europe/Berlin
)
)
[title] => A super nice Release
[digital_release] => DateTime Object
(
[date] => 2014-01-25 00:00:00.000000
[timezone_type] => 3
[timezone] => Europe/Berlin
)
[physical_release] => DateTime Object
(
[date] => 2014-01-25 00:00:00.000000
[timezone_type] => 3
[timezone] => Europe/Berlin
)
)
[2] => Array
(
[id] => 2
[artist] => Array
(
[id] => 14
[...]
)
[title] => Some other nice album
[digital_release] => DateTime Object
(
[date] => 2014-02-01 00:00:00.000000
[timezone_type] => 3
[timezone] => Europe/Berlin
)
[physical_release] => DateTime Object
(
[date] => 2014-02-01 00:00:00.000000
[timezone_type] => 3
[timezone] => Europe/Berlin
)
)
[...]
)
一些結果也許有人有一個有用的意見如何獲得這樣的輸出:)
我已經嘗試過使用AbstractQuery::HYDRATE_SCALAR
,附帶有點接近我想要的,但連接表的結果不是連接列的子數組。
預先感謝每一個有用的提示:)
問候
嘿Hooli,感謝這個樂於助人的評論! 因此,您的映射示例運行良好,但在操作中 - 當我創建新的發佈實例時 - 我不知道如何使用我的數據填充實體對象。我的意思是:新版本意味着一個單一的項目,但我的查詢構建了查詢中所有項目的列表。所以我得到了我的整個查詢(這是用QueryBuilder的結果)的結果我的$查詢,另一方面,我有這個不錯但空的實體對象... 也許一個愚蠢的問題,但:如何做你把這兩樣東西放在一起? – bquarta
@bquarta我編輯了我的帖子 – Hooli
對不起,對於最近的回覆:我嘗試了它,它適用於對象本身。但我無法將對象作爲jsonModel傳遞,因爲實體的屬性受到保護,因此json將爲空。 如果這最後一步將起作用,那麼一切都會好起來的。 – bquarta