CGridView question

Hello everybody,

i have this CGridView, and i have this dataProvider object:

$dataProvider=new CActiveDataProvider("Arma", array(








as you can see each Arma is related many to many to Classe.

the problem is that i’m just able to show Arma’s fields in my grid like this:

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




			"class" => "CLinkColumn",

			"labelExpression" => '$data->nome',

			"urlExpression" => '"profilo/".$data->id_arma',

			"header" => Yii::t("arma","LABEL-NOME"),



			"name" => "prezzo",

			"value" => '$data->prezzo." €"'





Anybody can help me out with creating another column with the all the Classe->nome occurrences for each Arma row?

Maybe comma separated since there can be many of them for each Arma. I’m digging through documentation but i can’t make it work. I think this can be a useful how-to even for other newbies like me in the same situation.

I hope i’ve made it clear enough to make sense to you!

Thanks in advance

I suggest creating a method in your model class to retrieve the related values and present them as a comma separated list as you mentioned.

//Arma.php (Model)

public function getClasses(){

  return implode(',', $this->classes); 


//then in your gridview, something to this effect


Someone should correct me if I’m wrong about this, but I think I heard this somewhere. If you create a function in the ActiveRecord class such as getSomething(), that you can then treat this as an attribute? Like $model->something instead of $model->getSomething()? Either way, I think using a get method like this should do the trick of collecting all the related fields and allowing you to put that into your gridview.

Alright, I shouldn’t have been lazy. What I was saying at the end is true.

//model class:

public function getSomething(){ return 'something'; }


echo $model->something;

//same as

echo $model->getSomething();

Thanks a lot!! the solution seems obvious now, but i just

needed someone to point me to the right direction!