user1916533
user1916533

Reputation: 101

How to add "order by" on a Magento Query

I installed a script for magento. It allows to add comments on orders. So it shows ONE comment on order grid.

The problem is that it doesn't sort comment by "created_at" column. I don't know how to set the order.

This is the portion of code:

 protected function _initSelect()
{
    parent::_initSelect();

    // Join order comment
    $this->getSelect()->joinLeft(
        array('ordercomment_table' => $this->getTable('sales/order_status_history')),
        'main_table.entity_id = ordercomment_table.parent_id AND ordercomment_table.comment IS NOT NULL',
        array(
            'ordercomment' => 'ordercomment_table.comment',
        )
    )->group('main_table.entity_id');

    return $this;
}

Thanks for your help.

Upvotes: 2

Views: 5346

Answers (2)

Nagamuthu
Nagamuthu

Reputation: 21

Replace this line in the above existing function. Add an ORDER BY.

$this->getSelect()->order('ordercomment_table.created_at', 'DESC');

Upvotes: 1

benmarks
benmarks

Reputation: 23205

protected function _initSelect()
{
    parent::_initSelect();

    // Join order comment
    $this->getSelect()->joinLeft(
        array('ordercomment_table' => $this->getTable('sales/order_status_history')),
        'main_table.entity_id = ordercomment_table.parent_id AND ordercomment_table.comment IS NOT NULL',
        array(
            'ordercomment' => 'ordercomment_table.comment',
        )
    )->group('main_table.entity_id');

    //Add ORDER BY
    $this->getSelect()->order(array('ordercomment_table.created_at DESC'));

    return $this;
}

Upvotes: 5

Related Questions