Default Sort Attribute for Doctrine Model

I was wondering if there is a way to declare the default order for my doctrine models.

eg.

I have a work model and it has photos . When I upload a work, all the photos associated with it are uploaded to $work->photos . When I show them, they are sorted by their identifiers.

It would be very convenient to declare a default order in another field or, possibly, to override the selection behavior.

I would prefer not to convert the photos to an array and use usort. Thanks.

+8
php doctrine
source share
3 answers

You can specify it in YAML as follows:

If this is the sort order for the field in the table itself, add:

 options: orderBy: fieldname 

where options: is at the same depth as the columns: or relations: entry. NB: capitalization orderBy: is vital; make no mistake, and you will not get any error, but you will not sort it.

If this is the sort order for the relationship, then within the relationship, you can skip the options: part and simply insert:

 orderBy: fieldname 
+9
source share

Ok, I got around this thanks to this post: http://www.littlehart.net/atthekeyboard/2010/02/04/sorting-relationship-results-in-doctrine-1-2-2/

In my case, the BaseWork.php file had the following modifications:

 public function setUp() { parent::setUp(); $this->hasMany('Photo as photos', array( 'local' => 'id', 'orderBy' => 'display_order', 'foreign' => 'work_id')); 

Anyway, it would be better to specify this in schema.yml, which I could not do.

+4
source share

I don’t know the first thing about the doctrine, but it looks like you can specify the order by clause by calling create ().

http://www.doctrine-project.org/documentation/manual/1_0/en/dql-doctrine-query-language:order-by-clause

0
source share

All Articles