bob dope
bob dope

Reputation: 479

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?

Upvotes: 4

Views: 10881

Answers (1)

Ocramius
Ocramius

Reputation: 25440

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;

Upvotes: 10

Related Questions