CGridview and relation

Ritmo model relations:


public function relations() {


        // NOTE: you may need to adjust the relation name and the related

        // class name for the relations automatically generated below.


        return array(

            'encomendaLinhas' => array(self::HAS_MANY, 'EncomendaLinha', 'style'),

            'image' => array(self::BELONGS_TO, 'Document', 'image'),

            'categoriaR' => array(self::BELONGS_TO, 'Categoria', 'categoria'),

            'teclados' => array(self::MANY_MANY, 'Teclado', 'ritmo_teclado(idritmo, idteclado)'),

        );

    }


public function search() {




        $criteria = new CDbCriteria;

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

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

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

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

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

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

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

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

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


        return new CActiveDataProvider($this, array(

                    'criteria' => $criteria,

                ));

    }



admin view:




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

	'id'=>'ritmo-grid',

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

	'filter'=>$model,

	'columns'=>array(


		'artist',

		'title',

		'price',

            array('header'=>'Categoria',

                'name'=>'categoriaR.name'),

            array('header'=>'Teclado',

                  'name'=>'teclados.model'),

		array(

			'class'=>'CButtonColumn',

		),

	),

)); ?>




Why categoriaR.Name shows up and teclados.model dont?

In your model, categoriaR relation returns a Categoria model, so categoriaR.name returns a value, while teclados relation returns an array of Teclado models, so you can’t refer to teclados.model

Ok.Well to obtain the teclado model for this ritmo what i must do?I have a RitmoTeclado model because it is a N-M relation

Well it depends :) what do you want to display in that column?

I want to display one column with the attribute brand.’-’.model of the teclado model

teclado model:


<?php


/**

 * This is the model class for table "teclado".

 *

 * The followings are the available columns in table 'teclado':

 * @property integer $idteclado

 * @property string $brand

 * @property string $model

 

 *

 * The followings are the available model relations:

 * @property Ritmo[] $ritmos

 */

class Teclado extends CActiveRecord

{

	/**

	 * Returns the static model of the specified AR class.

	 * @param string $className active record class name.

	 * @return Teclado the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return 'teclado';

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('model, brand', 'required'),

			array('model, brand', 'length', 'max'=>45),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('idteclado,brand,model', 'safe', 'on'=>'search'),

		);

	}


	/**

	 * @return array relational rules.

	 */

	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'ritmos' => array(self::MANY_MANY, 'Ritmo', 'ritmo_teclado(idteclado, idritmo)'),

		);

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'idteclado' => 'Idteclado',

                        'brand' => Yii::t('app','Brand'),

			'model' => Yii::t('app','Model'),

			

		);

	}

        

        public function getTecladoName(){

            

            return $this->brand.'-'.$this->model;

        }


	/**

	 * Retrieves a list of models based on the current search/filter conditions.

	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.

	 */

	public function search()

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;


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

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

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

		


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}

}

and ritmo_teclado model:


<?php


/**

 * This is the model class for table "ritmo_teclado".

 *

 * The followings are the available columns in table 'ritmo_teclado':

 * @property integer $idritmo

 * @property integer $idteclado

 */

class RitmoTeclado extends CActiveRecord

{

	/**

	 * Returns the static model of the specified AR class.

	 * @param string $className active record class name.

	 * @return RitmoTeclado the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}

        

         public function addTeclado(){

                          

            if($this->findByPk(array('idritmo'=>$this->idritmo,'idteclado'=>$this->idteclado))==null)

            {

                $this->save();

            }

        }

        

        public function revokeTeclado(){

            $ritmoteclado=$this->findByPk(array('idritmo'=>$this->idritmo,'idteclado'=>$this->idteclado));

            if ($ritmoteclado)

            {

             

               $ritmoteclado->delete();

            }

            

        }

        

         public function grantAllTeclados($idritmo){

            

            $this->revokeAllTeclados($idritmo);

            $teclados=Teclado::model()->findAll();

            foreach($teclados as $teclado ){

                $tecladoR=new RitmoTeclado;

                $tecladoR->idteclado=$teclado->idteclado;

                $tecladoR->idritmo=$idritmo;

                $tecladoR->addTeclado();

                   

                   

            }

        }

        

        public function revokeAllTeclados($idritmo){

            

            $this->deleteAll('idritmo='.$idritmo);

        }


	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return 'ritmo_teclado';

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('idritmo, idteclado', 'required'),

			array('idritmo, idteclado', 'numerical', 'integerOnly'=>true),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('idritmo, idteclado', 'safe', 'on'=>'search'),

		);

	}


	/**

	 * @return array relational rules.

	 */

	public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

		);

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'idritmo' => 'Idritmo',

			'idteclado' => 'Idteclado',

		);

	}


	/**

	 * Retrieves a list of models based on the current search/filter conditions.

	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.

	 */

	public function search()

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;


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

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


		return new CActiveDataProvider($this, array(

			'criteria'=>$criteria,

		));

	}

}

You should add a method in your Ritmo model:


public function getAllTecladosNames(){

    $result = '';

    $teclados = $this->teclados;

    foreach ($teclados as $teclado)

        $result .= $teclado->tecladoName . '<br>';

    return $result;

}

And in your view:


…

array('header'=>'Teclado',

         'value'=>'$data->allTecladosNames'),

with that:

Property "Ritmo.$data->allTecladosNames" is not defined.

and with :


array('header'=>'Teclados',

                'name'=>'allTecladosNames'),

            

i get the data but with <br> not encoded.I added ‘type’=>‘raw’ with success.Thanks

Are you sure you put


array('header'=>'Teclado',

         'value'=>'$data->allTecladosNames'),

It seems like you did


array('header'=>'Teclado',

         'name'=>'$data->allTecladosNames'),

Yes you are right.

Have you tried,




array('header'=>'Teclados',

                'value'=>'$data->getAllTecladosNames()'),



or defined class variable




public $allTecladosNames;



I have no explanation. But, I got the same problem and above was my workaround.

Solved

Another question.To display html in a column i must provide a function by the controller or model or can i set the html in the gridview value?

You can use HTML, but you need to set the "type" attribute to either "html" or "raw":

http://www.yiiframework.com/doc/api/1.1/CDataColumn#type-detail

http://www.yiiframework.com/doc/api/1.1/CFormatter




array(

    'header'=>$header,

    'type'=>'raw',

    'value'=>'<h1>BOO!</h1>',

)



Ok.But how to include in this html model values and php?

As you would normally:




array(

    'header'=>$header,

    'type'=>'raw',

    'value'=>'<h1>' . CHtml::encode($data->message) . '</h1>',

)



with that i get

Undefined variable: data


array(

    'header'=>$header,

    'type'=>'raw',

    'value'=>'"<h1>" . CHtml::encode($data->message) . "</h1>"',

)

And what abount it call a function inside the controller? I am trying to do this:

controller:


public function getPais($sigla){


            return 'test';

            }

	}


array('header'=>'Country',

                 

                'value'=>'$this->getPais($data->country)'

		),

but i get:

CDataColumn and its behaviors do not have a method or closure named "getPais".

The error is normal. In your context, $this refers to the column which is an instance of CDataColumn :)

Does your new question mean that the previous post does not answer your previous question? :)

yes i understand but how to call a controller function?