displaying different models in a CGridView or ClistView

Hello everybody!

One hour of googleing wasn’t sufficient. So I’ll ask my question here. Maybe somebody dealed with the same problem before.

I have different models in my project, let’s say:

User, Product, ForumPost

At the top of my page there’s a single search input field. You shall be able to use it finding anything: users, products or forum posts. Submitting the form leads to an action method of the SearchController. In this method I create CActiveDataProvider for all three models with the appropriate criterias:


$users = new CActiveDataProvider('User', ... )

$products = new CActiveDataProvider('Product', ... )

$forumposts = new CActiveDataProvider('ForumPost', ... )

I have now three instances of CActiveDataProvider. Handing all of them over towards the view file ‘index.php’ doesn’t make sense cause I have a single CListView waiting to display all search results mingled together and just ordered by e.g. creation date or relevance:


<h1>Meine Searchengefinden</h1>

<div class="list">

	<?php $this->widget('zii.widgets.CListView', array(

		'dataProvider'=>$dataProvider,

		'itemView'=> '_result_item.php',

		); ?>

	</div>

</div>

So what I want and probably need is to merge those three CActiveDataProvider into a single one because CListView just wants to have a single CActiveDataProvider.

Maybe it’s plain simple and I’m just confused by facing my code for too long. Displaying the resulting items of a search over two or more different models can’t be that complicated, eh?

Thanks in advance!

This topic (single search cross models) has been discussed quite a bit, I have never done anything like yet, though I thought about what I would do if I get chance to do it.

here is my two cents:

option 1 : create a search result model with basically following fields:

  • user-session, so not messed up with other user’s search

  • original model name, ‘user’

  • original model id, 123

  • original model search text, ‘john smith’, ‘camera’, etc.

then do your multiple model search and populate this table, render the result from this model;

option 2 : in your search create three dataproviders with different names (of course) and pass them all to your result view page

so in your result view page, render three listViews separately, something like:




<h1>found in users</h1>

<div class="list">

        <?php $this->widget('zii.widgets.CListView', array(

                'dataProvider'=>$dataProviderUser,

                'itemView'=> '_result_item_user.php',

                ); ?>

        </div>

</div>


<h1>found in product</h1>

<div class="list">

        <?php $this->widget('zii.widgets.CListView', array(

                'dataProvider'=>$dataProviderProduct,

                'itemView'=> '_result_item_product.php',

                ); ?>

        </div>

</div>


<h1>found in forumpost</h1>

<div class="list">

        <?php $this->widget('zii.widgets.CListView', array(

                'dataProvider'=>$dataProviderForumpost,

                'itemView'=> '_result_item_forumpost.php',

                ); ?>

        </div>

</div>