More Models In Dataprovider


In my application I use two models: Image, Video. I want to show recently added images and videos on one page.

What I have:

$images = Image::model()->findAll();

$videos = Video::model()->findAll();

$dataProvider= new CArrayDataProvider(array_merge($videos, $images));

and in view I use CListView. But this is unefficient and ugly solution, I dont know how to use pagination or sort merged data by time.

Is there any better solution how to merge two models to one DataProvider?

if those tables are similar, so create view in database that merges those records. Then create new view. if this is not solution use DAO.

A few more thoughts.

A model is supposed to represent a row in a table. Do images and videos need to be separate DB tables? Aren’t they both really just two different types of essentially the same content?

Read up on single table inheritance and alex makarov’s examples of how to code for it in Yii. By using this pattern you can keep everything in one DB table while having separate models for each subtype. In fact, you add a type field to the DB and when loading your model you choose the correct model based on content type. Its pretty slick.

Otherwise as the other guy said - perhaps just use DAO with a union statement.