How To Combine Multiple Dataproviders And Re-Arrange Then In Create_Time Desc?

Dear all,

I am doing a function which is kinda like facebook’s feed function.

I have multiple models, in this case, Answers, Questions and Replies.

I want the user see the latest answers, questions and replies which are MIXED and Ordered as create_time DESC.

I tried this but apparently the Data provider can not be used like this:


$news = array();         

$criteria=new CDbCriteria;

$criteria->order = 'create_time DESC';

$criteria->limit = 10;

$criteria->condition='owner_id=:owner_id';

$criteria->params=array(':owner_id'=>Yii::app()->user->id);


$newquestion=Questions::model()->findAll($criteria);

$newanswer = Answers::model()->findAll($criteria);

$newreply = Answers::model()->findAll($criteria);


array_push($news,$newquestion);

array_push($news,$newanswer);

array_push($news,$newreply);


     $newsDataProvider=new CArrayDataProvider($news,array(

     'sort' => array(

         'defaultOrder' => 'create_time DESC',  // this is it.

         ),

     'pagination' => array('pageSize' => 6),

));

Is there a way to this stuff?

Thanks so much!

Hi jiaming

Could you please post the table structure as this could be caused by a field mismatch. As well as your sorting is incorrect with the CArrayDataProvider the sorting needs to be declared as follows.




$newsDataProvider=new CArrayDataProvider($news,array(

     'sort' => array(

         'attributes' =>array(

             'create_time DESC',

         )

      ),

     'pagination' => array('pageSize' => 6),

));



Also for the type of application your building and the possible amount of data that might get trough this I’ll recommend keeping this in the database with a UNION function ether with a stored procedure or with CSqlDataProvider. If you need any more detailed help to be afraid to ask.

Hi,

Thanks so much for your help.

You are right. Since these 3 tables has differnet structure, so i can’t combine them into one CarrayDataprovider.

Would you please show me more details about how to achieve this in the sqlDataprovider way with UNION?

These 3 tables has different structures but all of them have a field called “create_time”. That’s what i want them to be ordered…

Thanks so much for your help!!!

I’ll need to see the table columns to be able to help you with the union query. But maybe a better solution performance wise is to create a additional table called something like “feed_item” that stores meta data that will be used for filtering and ordering with a generic relation to the table:

Example of such a table:


user_id | create_time         | object_name | object_id |

      1 | 2012-09-17 09:48:00 | Questions   |         1 |

This way you’ll easily be able to filter and order quickly and get all the relevant information with out a complicated query. Just add a afterSave() method or behaviour that will record the feed_item.

Thanks so much for your help. I decided to make a feed table and use cron to clean it several days. e.g, 7days

Cool glad I could help.