In question at Default sort attribute for Doctrine Model a .yml
is suggested to define a default sorting for a collection-valued association.
I would like to have my models fetched by a default sorting, like following:
Foo:
columns:
...
options:
orderBy: bar DESC
What is the annotation equivalent of this YAML-based setup?
EDIT: This is not possible with defaults. Entities fetched from repositories are fetched by the provided sorting criteria:
$entities = $entityRepository->findBy(array(), array('field' => 'ASC'));
This, DQL and the Criteria API are the current ways of fetching entities with a given sorting criteria.
What the question at " Default sort attribute for Doctrine Model " is about is the sorting of collection-valued associations, which has nothing to do with direct fetching of entities from repositories.
For those associations, the annotation-equivalent of " Default sort attribute for Doctrine Model " is following (original answer):
As of the official annotations documentation for Doctrine 2 ORM, the annotation for default sorting conditions of collection-valued associations @OrderBy({"field" = "ASC", "otherField" = "DESC"})
.
Here's how you would use it:
/**
* @ORM\OneToMany(targetEntity="Users")
* @ORM\OrderBy({"username" = "ASC"})
*/
protected $users;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With