2015-12-22 75 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'相关的实体字段,因为这个实体并不总是在水合时加入。在这种情况下,加入应该是强制性的。 –