mysqlsqlyiicactivedataprovider

SQL sorting: order blocks with DESC - but each block is sorted ASC within itself


I have a SQL table with messages and their date of publication.

Let's call these messages M1, M2, ... M99. M1 being the newest message.

I want to display them (i'm using Yii 1.1 framework) in a whatsapp way: The newest message M1 is on the bottom and above it is the second newest M2 message, etc.

But I want to display them by blocks of a fixed number of message (let's say 5), and I'll be loading previous message if requested. So when loading the page you should see :

(load more button)

M5

M4

M3

M2

M1

When clicking once on the load more button, I will have then, from top to bottom :

(load more button)/M10/M9/M8/M7/M6/M5/M4/M3/M2/M1

I'm trying to achieve this using an ActiveDataProvider on Yii, however for this, I would need to have data sorted like this :

M5,M4,M3,M2,M1,M10,M9,M8,M7,M6,M15,M14,M13,M12,M11, etc.

I'm not sure how to achieve that kind of sorting, even using limit and offset.

I'd be happy if anyone could help me on this, thanks!

edit: Here is the ActiveDataProvider I'm using

public function getMessagesDataProvider()
{
    $dataProvider = new CActiveDataProvider(Message::model(), array(
        'criteria' => array(
            'condition' => 't.inquiry_thread_id = :threadId',
            'params' => array(':threadId' => $this->id),
            'order' => 't.created_at DESC',
        ),
        'pagination' => array('pageSize' => 5, 'pageVar' => 'page'),
    ));
    $dataProvider->getData();
}

And in my view

<?php $this->widget('\common\widgets\ListView', array(
    'dataProvider' => $messagesDataProvider,
    'pager' => array(
        'class' => '\common\widgets\LoadMorePager',
        'canBeReloadedByAjax' => true,
    ),
    'template' => '{pager}{items}',
    'itemView' => '/message/_messageItem',
    'itemsCssClass' => 'pager-container',
    'pagerCssClass' => 'inquiry-message-button',
)); ?>

Solution

  • You can use setData() and getData() to reverse order of items in data provider:

    $dataProvider->setData(array_reverse($dataProvider->getData()));
    

    $dataProvider->getData() will return page items in reversed order.