1

Let A, B, C be associated entities. I'm aiming for A->getBs() to return a collection of B ordered by two properties: B.createdAt and B.C.displayOrder.

First works fine, second does not: I've tried "c.displayOrder" = "asc", but this has been unsuccessful (Unrecognized field: c.displayOrder).

Is there any way to order by a property from another related entity?

The other option I could imagine is overriding the function getBs(), but I wouldn't know how to create a custom DQL query in there to accommodate for my special need. I've read that using the EntityManager inside an Entity is bad practice.

A
/**
  * @ORM\OneToMany(targetEntity="B", mappedBy="a")
  * @ORM\OrderBy({"createdAt" = "asc", "c.displayOrder" = "asc"})
  */
-id
-getBs()


B
/**
 * @ORM\ManyToOne(targetEntity="A")
 * @ORM\JoinColumn(referencedColumnName="id")
 */

/**
 * @ORM\ManyToOne(targetEntity="C")
 * @ORM\JoinColumn(referencedColumnName="id")
 */
-id
-createdAt

C
-id
-description
-displayOrder

Using doctrine/orm v2.4.5, symfony-standard-edition 2.3.20

snwflk
  • 3,341
  • 4
  • 25
  • 37

1 Answers1

0

Take a look to these 5 solutions described at question below:

Ordering Doctrine Collection based on associated Entity when it is not possible to use the @orderBy annotation

Community
  • 1
  • 1
manix
  • 14,537
  • 11
  • 70
  • 107
  • 1
    In my research, I had come across those options. It's good to know there aren't better ways to do it. I chose option 4 - implementing a custom `uasort` inside my `getBs()` function. This should be fairly ok design. I accomplished the two-factor sorting by using an if-else structure inside `uasort`. – snwflk Oct 05 '14 at 10:12