Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove DataObject default sort

I have a DataObject with a default sort:

class Author extends DataObject {
    private static $db = array('Name' => 'Varchar');
    private static $default_sort = 'Name ASC';
}

I want to Author::get() the data but with no sort.

so given...

Author::create(array('Name' => 'DEF'))->write();
Author::create(array('Name' => 'ABC'))->write();

This would output ABC then DEF using the sort, but if the sort was cleared I'd expect DEF then ABC.

I've tried Author::get()->sort('') and Author::get()->sort(null) but both return ABC then DEF again.

Note The reason I'm asking is that I have a complex query (using joins) where this is causing an issue and I'm getting an error with it if I try and set the sort too.

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ORDER BY "_SortColumn0" ASC' at line 1

So the solution, and what I'd like to know anyway, is how to clear the default sort for the specific Author::get() while keeping the existing default sort on the DataObject?

like image 279
Barry Avatar asked Aug 17 '16 10:08

Barry


1 Answers

I think the only way to unset the sort would be to manipulate the DataQuery that underpins the DataList which is returned when running Author::get().

$dq = Author::get()->dataQuery();
$dq->sort(null, null, true);
$authors = Author::get()->setDataQuery($dq);
like image 88
Dan Hensby Avatar answered Sep 30 '22 10:09

Dan Hensby