Show query result in clistview and cgridview

Hi guys,

I wan to show the query result in cgridlist and clistview?? how to show in view?? any suggestion?

        $user = Yii::app()->db->createCommand()


                ->from('user u')

                ->join('company c', '')

                ->where('', array(':email' => $userId))



thank, =)


Yii works with ORM… use it!. gridview and listview needs dataprovider. so if u wanna use sql then read CSqlDataProvider.

Hi Wartex,

how to get the result In CGridView from other table? i wan show user and company info in 1 cgridview.


    $this->widget('zii.widgets.grid.CGridView', array(

        'id' => 'detail-grid',

        'dataProvider' => $dataProvider,

        'filter' => $dataProvider,

        'columns' => array(




            'company_name', //get comp_name at company table

id,user_name,email is from user table and company_name from company table.

You can use relational AR in grid/listviews just like doing a plain AR query.

In order for this example to work you need to set relations. See in guide:

Since you have 2 models here, User and Company…

In User model, set company relation (named companies). User BELONGS_TO Company

  'companies'=>array(self::BELONGS_TO, 'Company', 'user_id')

In Company model, set user relation (named users). Company HAS_MANY User.

'users'=>array(self::HAS_MANY, 'User', 'user_id')

Then you can do this:

$this->widget('zii.widgets.grid.CGridView', array(

        'id' => 'detail-grid',

        'dataProvider' => $dataProvider,

        'filter' => $dataProvider,

        'columns' => array(




            //'company_name', get comp_name at company table

            'companies.company_name' //uses relation in model to query related data

Hi dniznick,

How to write the code in controller view? As below? plus if i wan filter the name, how to write in search() condition?



   $dataProvider = new CActiveDataProvider('company');

        $this->render('company_view', array(

            'dataProvider' => $dataProvider,

    public function search()



        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id, true);

        $criteria->compare('user_name', $this->user_name, true);

        $criteria->compare('company_name', $this->company-name, true);   //get the value from companies.company_name


        $criteria->with = array('company');

        $criteria->together = true;

        return new CActiveDataProvider(get_class($this), array(

            'criteria' => $criteria,



Thanks, anyone can give me ideas… =)

Check this (Zaccaria’s post):