Pierre de LESPINAY
Pierre de LESPINAY

Reputation: 46168

Doctrine order by related entity field

I have 3 entities:

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

As you can see, an investment has multiple payouts which are attached to one operation

I'd like to get investment.payouts ordered by payout.operation.effectiveDate.

I tried to use orderBy: {operation.effectiveDate: 'ASC'} but it doesn't work.

So the question is:
How can I order investment.payouts by payout.operation.effectiveDate ?

Upvotes: 2

Views: 2245

Answers (1)

abdiel
abdiel

Reputation: 2106

Why not use a repository function?

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();
}

Upvotes: 4

Related Questions