0

I have two models: Event and Match. An event can have many matches. My Match model has $order = array('match_order' => 'asc'). The match_order column in my matches database table is just a simple INT column to order matches.

I have a controller method that updates the order of matches. However, because I fetch matches through the event, they don't seem to come in ascending by match_order, but by the id column. Here is an example call:

<?php
class EventsController extends AppController {
    public function view($id) {
        $event = $this->Event->find('first', array(
            'conditions' => array(
                'Event.id' => $id
            ),
            'contain' => array('Match')
        ));
    }
}

All the related matches come back as expected, but just not in the right order. I've cleared my model caches, is there anything else I need to do to get the matches order by the value in the Match model?

tereško
  • 58,060
  • 25
  • 98
  • 150
Martin Bean
  • 38,379
  • 25
  • 128
  • 201

1 Answers1

0

Solved it. Forgot I could specify order in my model associations, so I've added the following to my Event model:

<?php
class Event extends AppModel {
    public $hasMany = array(
        'Match' => array(
            'order' => array(
                'match_order' => 'asc'
            )
        )
    );
}
Martin Bean
  • 38,379
  • 25
  • 128
  • 201
  • You can also specify the order in your find query, simply add an "order" key to the options array specifying the desired order. – elboletaire Sep 24 '12 at 10:50