SSbits - Home page
Site by Carbon Crayon
Submit a Post >

Tutorials - Big bits of code to help you do more

Sorting on a related DataObject in ModelAdmin

Sometimes, in ModelAdmin, we want to be able to have a different default sort.

Specifically, like in cases where a has_one is the preferred sorting. For example, we have a warehouse. In our DataObjects, we have the object "Lanes", has_many Products. And we have Products, has_one "Lanes". When we're browsing our Products in the admin, we want to have them default sorted by their Lane, not their name.

Just saying default_sort = 'Lanes'; sadly does not do the job. It'll give you reddish-screens and stuff. Which is not what we like.

Here's a (quite simple actually) solution.

In the ModelAdmin we've made (ProductAdmin extends ModelAdmin),

class Products extends DataObject {
    
    public static $db = array(
        'Title' => 'Varchar(255)',
        'Price' => 'Currency',
    );
    
    public static $has_one = array(
        'Lane' => 'Lanes',
    );
    
    public static $default_sort = 'Price ASC';
       
    public function getCMSFields(){
        $fields->addFieldsToTab('Root.Main', array(
                new TextField('Title', _t($this->class . '.TITLE', 'Title *NYT*')),
                new CurrencyField('Price', _t($this->class . '.PRICE', 'Price *NYT*'))
            )
        );
        return $fields;
    }
    
}

And the same for our Lanes-object. Offcourse.

 

class Lanes extends DataObject {
    
    public static $db = array(
        'Title' => 'Varchar(255)',
        'LaneNumber' => 'Int',
    );
    
    public static $has_many = array(
        'Products' => 'Products',
    );
    
    public static $default_sort = 'Price ASC';
       
    public function getCMSFields(){
        $fields->addFieldsToTab('Root.Main', array(
                new TextField('Title', _t($this->class . '.TITLE', 'Title *NYT*')),
new DropdownField('LaneNumber', _t($this->class . '.LANENUMBER', 'Lane *NYT*), array(1 => 1, 2 => 1)) ); return $fields; } }

 

Ok, that's all simple and obvious. Just DataObjects. (Ignore the example-values and such).

Now, in the ModelAdmin, where we manage the Products, it can't be sorted by Lane Number. It won't do that.

Unless...

 

class ProductAdmin_CollectionController extends ModelAdmin_CollectionController{
    
    function getSearchQuery($searchCriteria) {
        if(strpos($searchCriteria['url'],'Products')){
            $query = parent::getSearchQuery($searchCriteria)->leftJoin( 'Lanes', 'Products.LaneID = Lanes.ID');
            return $query->orderby('Lanes.LaneNumber ASC');
        }
        else{
              return parent::getSearchQuery($searchCriteria);
        }  
    } 
}

 

This simple extension on the ModelAdmin sorts our products depending on which lane they're in.

Simon Erkelens avatar

Simon Erkelens

Normal people believe that if it ain't broke, don't fix it. Engineers believe that if it ain't broke, it doesn't have enough features yet.

  • Sascha R.
    02/10/2013 10:10am (10 months ago)

    For SS3 the adjustments are pretty simple:

    public function getList(){
    $list = parent::getList()->leftJoin('Lanes', 'Products.LaneID = Lanes.ID');
    $list->sort('Lanes.LaneNumber ASC');
    }

Post a comment ...

You cannot post comments until you have logged in. Login Here.

Advertisement

Site of the Month

Find SSbits on

Top Contributers

Rank Avatar Name
1 article image Aram Balakjian
2 article image Daniel Hensby
3 article image Marcus Dalgren
4 article image Hamish Campbell
5 article image njorndare
6 article image Ty Barho
7 article image Martijn van Nieuwenhoven
8 article image Darren-Lee
9 article image Roman Schmid
10 article image Matt Clegg

View full leaderboard


Advertisement