Friday, July 19, 2013

Sorting related records

By default I assume for example Gallery hasMany Image.

When I view the specific gallery (as admin, straight tables as baked) I see all the related images. Now I built in a drag and drop to order the images how I want, but they are being sorted by Cake I assume by the id. So even though I have the order set as 1, 2, 3 they get displayed as say 2,3,1

Is there a built in way to sort by a specific field for the related Image(s) or simply need to build a custom method to fetch as I want it?

 

Standard bake produces:

public function admin_view($id = null) {

                                $this->Gallery->id = $id;

                                if (!$this->Gallery->exists()) {

                                                throw new NotFoundException(__('Invalid %s', __('gallery')));

                                }

                                $this->set('gallery', $this->Gallery->read(null, $id));

                }

 

Simply replace $this->set('gallery', $this->Gallery->read(null, $id)); with a custom find

$this->set('gallery', $this->Gallery->getGallery($id)); and do the sort Image within that? Or is there a built in method to take care of how you want related records sorted?

 

Thanks,

 

Dave

No comments: