Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default sort for doctrine model with annotations

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?

like image 347
bob dope Avatar asked Dec 26 '22 08:12

bob dope


1 Answers

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;
like image 133
Ocramius Avatar answered Jan 04 '23 18:01

Ocramius