I want to sort my model’s associated ArrayCollection with a quotient, like this (I know the following code doesn’t work):
/**
* @OneToMany (targetEntity="Review", mappedBy="product")
* @OrderBy ({"voted_up / voted_down" = "DESC"})
*/
protected $reviews;
Is something this possible directly in the model definition or do I need to simply use a sort() on the ArrayCollection when requesting the data?
With Doctrine 2.1 you can do that directly in the model definition, but not with @OrderBy. You can define DQL snippets at model level, like stated in the 2.1 Beta release notes:
Named DQL Queries in Metadata: You can add dql queries in the mapping files using @NamedQueries(@NamedQuery(name=”foo”, query=”DQL”)) and access them through $em->getRepository()->getNamedQuery().
As such you can create your DQL query with the ORDER BY keywords, something like:
So, I imagine you add this annotation to the model definition, something like:
And then on your code call:
I didn’t test this, but you get the idea.