2015-04-24 35 views
2

我目前遇到了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,附帶有點接近我想要的,但連接表的結果不是連接列的子數組。

預先感謝每一個有用的提示:)

問候

回答

1

你的問題是你的映射這不是好,我想你還沒有釋放和藝術家之間的關係。

您必須在Artist和Release之間設置OneToMany關係。就像這樣:

On artist side 
/** 
* @ORM\OneToMany(targetEntity="Release\Entity\Release", mappedBy="artist") 
*/ 
protected $release; 

And Release side 
/** 
* @ORM\ManyToOne(targetEntity="Artist\Entity\Artist", inversedBy="release") 
* @ORM\JoinColumn(name="artist", referencedColumnName="idArtist", onDelete="RESTRICT") 
*/ 
protected $artist; 

不要忘記藝術家方實例化這樣一個集合:

public function __construct() 
{ 
    $this->release= new ArrayCollection(); 
} 

本用途:

use Doctrine\Common\Collections\ArrayCollection; 

有了這個,你可以設置你的版本實體並將藝術家設置爲藝術家對象。你會得到你想要的結果。

$release = new Release; 
$artist = new Artist; 
$release->setArtist($artist); 

編輯 當你有你的數據,你必須與它來填充你的實體。

如果你有一個表單,你必須使用DoctrineHydrator來實現這一點,你的對象將從你的表單中正確地水化。這可能需要深入理論文檔;) 如果您的數據來自其他任何地方,您可以使用實體的setter填充您的實體。

對於e.g:

$relaseEntity->setDate($data['date']); // Be carefull to Datetime object for this 
$releaseEntity->setName($data['name']); 

並繼續...

+0

嘿Hooli,感謝這個樂於助人的評論! 因此,您的映射示例運行良好,但在操作中 - 當我創建新的發佈實例時 - 我不知道如何使用我的數據填充實體對象。我的意思是:新版本意味着一個單一的項目,但我的查詢構建了查詢中所有項目的列表。所以我得到了我的整個查詢(這是用QueryBuilder的結果)的結果我的$查詢,另一方面,我有這個不錯但空的實體對象... 也許一個愚蠢的問題,但:如何做你把這兩樣東西放在一起? – bquarta

+0

@bquarta我編輯了我的帖子 – Hooli

+0

對不起,對於最近的回覆:我嘗試了它,它適用於對象本身。但我無法將對象作爲jsonModel傳遞,因爲實體的屬性受到保護,因此json將爲空。 如果這最後一步將起作用,那麼一切都會好起來的。 – bquarta

相關問題