Search code examples
phpzend-frameworkzfdatagrid

How to show zend framework data grid data sorted by "date" as default?


How to show zend framework data grid data sorted by "date" as default? When i get on the page, i wish to see data grid sorted by time as default, without getting params in URL like .../order/created_DESC/

$testimonials = new Application_Model_DbTable_Testimonials();
        $source = new Bvb_Grid_Source_Zend_Table($testimonials);

Thanks.

I solved this by passing to datagrid $select instead of $object

$testimonials = new Application_Model_DbTable_Testimonials();
        $source = new Bvb_Grid_Source_Zend_Table($testimonials);

is now

$testimonials = new Application_Model_DbTable_Testimonials();
        $testimonials->setSortOrder('created');
        $source = new Bvb_Grid_Source_Zend_Select($testimonials->getTestimonials());

Solution

  • It's hard to see what your class is doing since you didn't posted that one.

    But when it's usess the fetchAll() function you can do two things:

    Option 1

    When there is an fetchAll()-call in the Grid class you can make this one:

    $testimonialsTable->fetchAll(null, 'date'));
    

    Option 2

    You can re-write the fetchAll-Method in the Application_Model_DbTable_Testimonials class, when you make

    public function fetchRow($where = null, $order = null, $offset = null)
    {
        if ($order===null) : $order='date'; endif;
    
        return parent::fetchAll(where, $order, $offset)
    }
    

    Please notice in the last example fetchess always will be default sorted by the date-field.