How To Get Data From Different Table In Cgridview?

I have 3 tables. car_types: id | main_image | title car_type_classifiers: id | car_type_id | classifier_id classifiers: id | class

I want to display a CGridView so there are columns: Title | class. But Classes can be many for one car_type. I tried to search online, but coudnt understand those $criteria->compare() functions in model search function.

I want those to show up as little lists. How can I do that? my view:




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

    'id'=>'car-type-grid',

    'dataProvider'=>$model->search(),

    'filter'=>$model,

        'columns' => array(

                'title'

    ),

));

my controller:


  public function actionIndex()

{

    $model=new EeCarTypes('search');

    $model->unsetAttributes();


    $this->render('index',array('model'=>$model));

}

and my model:


    public function search()

{

    // @todo Please modify the following code to remove attributes that should not be searched.


    $criteria=new CDbCriteria;


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

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


    return new CActiveDataProvider($this, array(

        'criteria'=>$criteria,

    ));

}

Do u want just to display data from other tables, or U also want to search ?

I want to search each car_types->id, where it is equel to car_type_classifiers->car_type_id

and render

classifiers->class where

classifiers->id match car_type_classifiers->classifiers_id. I hope you can understand it.

can you post an example of what your tying to do? Maybe a picture? i.e. are you trying to show the car name with a picture and car models?

How is the info stored? Is all the info in different tables? if so post the relations if you have them. Just a little bit more info will go a long way here.

I somehow managed to make it work as I like, but I have an issue that filters doesnt work when they are added. They just doesnt update.

What I did was:

controller:


public function actionIndex()

    {

        $model=new EeCarTypes('search');

        

        $model->unsetAttributes();  // clear any default values

        if(isset($_GET['EeCarTypes']))

            $model->attributes=$_GET['EeCarTypes'];

        


        $this->render('index',array('model'=>$model));

    }



model:


   public $car_type;

    public $classifiers;

    public $main_image;

    public $assigned_images;




	public function attributeLabels()

	{

		return array(

			'id' => 'ID',

			'car_type' => 'Car Type',

            'classifiers' => 'Classes',

            'main_image' => 'Main Image',

            'assigned_images'=>'Assigned Images',

		);

	}




	public function search()

	{

		// @todo Please modify the following code to remove attributes that should not be searched.


		$criteria=new CDbCriteria;


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

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

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

        $criteria->compare('main_image',$this->main_image);

        $criteria->compare('assigned_images',$this->assigned_images);




		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}

and then I added functions to that model:


public function classifiers($id)

    {

        $model = EeCarTypeClassifiers::model()->findAllByAttributes(array('car_type_id'=>$id));

        $list = "<ul>";

        foreach ( $model as $item)

        {

            $tempModel = eeClassifiers::model()->findByPk($item->classifier_id);

            $list .= '<li>'.$tempModel->classifier_name.'</li>';

        }

        $list .= "</ul>";

            return $list;


    }


    public function main_image($id, $image)

    {

        $url = Yii::app()->request->getBaseUrl( true ) . '/images/upload/' . $id . '/' . $image;

        $image=CHtml::image( $url, 'no image', array(

            'class' => 'uploaded-images',

         ));

        return $image;

    }


    public function assigned_images($id)

    {

        $cntCriteria = new CDbCriteria();

        $cntCriteria->condition = "car_type_id = :id";

        $cntCriteria->params[':id'] = $id;

        $Count = EeCarTypeView::model()->count($cntCriteria);

        return $Count;

    }

and of course the view:




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

    'id'=>'car-type-grid',

    'dataProvider'=>$model->search(),

       'columns' => array(

            array('name'=>'main_image','type'=>'html','filter'=>false,

                'htmlOptions'=>array("style"=>"width: 100px;"),

                'value'=>'$data->main_image("$data->id","$data->car_type")'),


            array(

                'name'=>'title',

                'value'=>'$data->title',

            ),

            array('name'=>'classifiers','type'=>'html','htmlOptions'=>array(

                "class"=>'list-box',),


                'value'=>'$data->classifiers("$data->id")'),


            array('name'=>'assigned_images','value'=>'$data->assigned_images("$data->id")'),

            array(

                'class'=>'CButtonColumn',

                'template'=>'{delete}{update}',

            ),

        ),

));