15

How to change default entity order in SonataAdminBundle for list action?


answer :) add this to your admin class

protected $datagridValues = array(
    '_page' => 1,
    '_sort_order' => 'DESC', // sort direction 
    '_sort_by' => 'id' // field name 
);

2 Answers 2

9

It is better not to override constructor. But you can override the Admin::configure() method and set some element of the datagridValues array.

See in example:

public function configure()
{
    parent::configure();

    $this->datagridValues['_sort_by']    = 'name';
    $this->datagridValues['_sort_order'] = 'DESC';
}
2
  • 2
    To be safe, I would also add parent::configure(). Aug 13, 2013 at 1:55
  • 2
    This method is empty. it exists for just such situations. If I understand correctly. But you're right, will be safer to call the parent method. I adjusted the example. Aug 14, 2013 at 21:25
7

You can add another sort order or set a default one via the constructor like this:

public function __construct($code, $class, $baseControllerName)
{
    parent::__construct($code, $class, $baseControllerName);

    if (!$this->hasRequest()) {
        $this->datagridValues = array(
            '_page'       => 1,
            '_sort_order' => 'ASC',      // sort direction
            '_sort_by'    => 'artist_id' // field name
        );
    }
}
2
  • Warning: Missing argument 1 for Neon\CoreBundle\Controller\OrderAdminController::__construct()..
    – rtyshyk
    Nov 17, 2011 at 11:35
  • put please here list of namespaces you used Dec 14, 2011 at 12:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.