2015-12-22 99 views
1

我有3個實體:學說爲了通過相關實體領域

My\Bundle\Entity\Investment: 
    type: entity 
    fields: 
#  ... 
    oneToMany: 
     payouts: 
      targetEntity: My\Bundle\Entity\Payout 
      mappedBy: investment 
      orderBy: {operation.effectiveDate: 'ASC'} 

My\Bundle\Entity\Payout: 
    type: entity 
    fields: 
#  ... 
    manyToOne: 
     investment: 
      targetEntity: My\Bundle\Entity\Investment 
      inversedBy: payouts 
     operation: 
      targetEntity: My\Bundle\Entity\Operation 
      inversedBy: payouts 

My\Bundle\Entity\Operation: 
    type: entity 
    fields: 
     effectiveDate: 
      type: datetime 
#  ... 
    oneToMany: 
     payouts: 
      targetEntity: My\Bundle\Entity\Payout 
      mappedBy: operation 

正如你所看到的,investment有多個payouts被連接到一個operation

我想獲得investment.payouts通過有序payout.operation.effectiveDate

我試圖使用orderBy: {operation.effectiveDate: 'ASC'},但它不起作用。

所以,問題是:
如何訂購通過payout.operation.effectiveDateinvestment.payouts

+0

你能告訴我你正在執行的查詢的代碼嗎? –

+0

'$ em-> find('MyBundle:Investment',42) - > getPayouts()' –

回答

2

爲什麼不使用存儲庫功能?

public function getPayouts() { 

    $em = $this->getEntityManager(); 

    $sql = 'SELECT i, p, o ' 
      . 'FROM AppBundle:Investment i ' 
      . 'JOIN i.payouts p ' 
      . 'JOIN p.operation o ' 
      . 'ORDER BY o.effectiveDate '; 

    $query = $em->createQuery($sql); 

    return $query->getResult(); 
} 
+2

是的,您只能使用'orderBy'按子實體直接定義字段。否則,你必須使用查詢來做到這一點。 – Ryan

+0

是的,我將不得不使用存儲庫...謝謝。我想我們不能'orderBy'相關的實體字段,因爲這個實體並不總是在水合時加入。在這種情況下,加入應該是強制性的。 –