2016-07-22 20 views
2

該查詢效果很好,在我基於舊的基於PDO的代碼中,當查詢連接的表時,我能夠在父模型中嵌套相關的表模型。使用此代碼:如何在Doctrine中將另一個實體的連接表的結果實體封裝起來?

$qry = $em->createQueryBuilder() 
      ->select('l', 'a') 
      ->from('AppBundle:Hflogs', 'l') 
      ->leftJoin('AppBundle:HflogSwLocations', 'a') 
      ->where($em->createQueryBuilder()->expr()->notIn('l.id', $sub->getDQL())) 
      ->orderBy('l.submitted', 'DESC') 
      ->setMaxResults(50); 

     $logs = $qry->getQuery()->getResult(); 
     dump($logs); die(); 

架構的一部分,這個查詢利用,是我想一個平行關係,Hflog不包含關係的位置,而不是我使用的連接器表,這是專門針對Hflog只有地點。

enter image description here

學說annotaions爲HflogSwlocations:

/** 
    * @var \AppBundle\Entity\Hflogs 
    * 
    * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Hflogs") 
    * @ORM\JoinColumns({ 
    * @ORM\JoinColumn(name="hflog_id", referencedColumnName="id") 
    * }) 
    * @SerialGroups({"public"}) 
    * 
    */ 
    private $hflog; 

/** 
* @var \AppBundle\Entity\SwLocations 
* 
* @ORM\ManyToOne(targetEntity="AppBundle\Entity\SwLocations") 
* @ORM\JoinColumns({ 
* @ORM\JoinColumn(name="sw_loc_id", referencedColumnName="id") 
* }) 
* @SerialGroups({"public"}) 
* 
*/ 
private $swLoc; 

返回此:

array:51 [▼ 
    0 => Hflogs {#450 ▶} 
    1 => HflogSwLocations {#569 ▶} 
    2 => HflogSwLocations {#413 ▶} 
    3 => HflogSwLocations {#420 ▶} 
    4 => HflogSwLocations {#437 ▶} 
    5 => HflogSwLocations {#558 ▶} 
    6 => HflogSwLocations {#555 ▶} 
    7 => HflogSwLocations {#442 ▶} 
    8 => HflogSwLocations {#584 ▶} 
    9 => HflogSwLocations {#587 ▶} 

HflogSwLocations屬於HFlogs。

我可以得到我需要的東西,但提供結果的方式對我來說似乎並不直觀。如果Hflogs具有HflogSwLocations,那麼我想要將Hloglogs作爲關係實體的一部分進行封裝,並返回HFlogSwLocations。如何使用Doctrine查詢完成此操作?

+0

看看如何使用DQL進行左連接。 – Cerad

回答

0

您不應指定要在查詢中檢索'a'。您只需要檢索HfLogs。 Then it's all about lazy loading

+0

你是不是在檢索'a'是什麼意思?讓它脫離選擇方法?我應該使用leftJoin方法嗎? – cj5

+0

在您的查詢生成器的select()中,您正在選擇HfLogs(別名爲'l'和一些位置(用'a'別名)。您不需要執行左連接也不會提到'a'你的選擇列表,然後,當你訪問你的HfLogs屬性時,你可以直接在你的對象上使用這些位置。 – MeuhMeuh

+0

你能否爲我提供一個我的查詢構建的重寫? – cj5

相關問題