Concatenate values in CGridView with relation model

Hi All,

I’m very new to the Yii framework but i’ve been loving it so far.

I have 3 tables i’ve created relational model with.(customer, sales_rep, commissions)

I have my index view i changed to use CGridView.

I’m need to concatenate 2 values(first_name, last_name) from the customer model in the commissions index view.

Here’s my actionIndex method


	public function actionIndex()

	{

		$dataProvider=new CActiveDataProvider('Commissions');

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

			'dataProvider'=>$dataProvider,

		));

	}



I can show individual values as shown bellow:




		array(

			'name'=>'salesrep.first_name',

			'header'=>'Sales Rep',

		),



but i can’t seem to get more than one in a column.

I tried using ‘value’ and $dataProvider->model->customer->first_name but i get and error ‘can’t convert to string’

I’ve also tried passing the customer model over via the controller and using $customer->first_name but that just gives a blank field.

Is there any way to concatenate ‘name’ or do i need to use ‘value’.

Let me know

Thanks in advanced!!

Oliver

I am new to Yii as well but I did the following to concatenate first and last name fields for a dropdownlist

in my model:




public function getConcatened()

    {

        return $this->firstName.' '.$this->lastName;

    }



In my view:




echo $form->dropDownList($model,'coach_id',CHtml::listData(Coach::model()->findAll(),'id','concatened'));



For you you should be able to set the ‘value’=>‘Model::model()->concatened()’ though I haven’t tested it this way.

You can use the value field for that:




array(

  'name'=>'salesrep.first_name',

  'value'=>"$data->first_name . ' ' . $data->last_name",

),



Thanks for the replies. They’ve given me some ideas to try.

One of the problems i’m running into here is i’m trying to access an attribute from another model(i’ve got relations set up).

@Da:Sourcerer When i try your suggestion i get Undefined variable: data when i use $dataProvider instead i get "Property "CActiveDataProvider.first_name" is not defined."

I’ve also tried $model->customer->first_name but that throws the error "Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /var/www/html/yii/framework/base/CComponent.php(619) : eval()'d code on line 1 Call Stack: "

Any Thoughts?

Thanks

Oliver

Success!!! I was able to figure it out. I had to do some changing around of single and double quotes but it works now.

I’m now able to show concatenated attributes from other related models.

In the bellow example i am able in the commissions/admin view and getting attributes(first_name, last_name) from the customer model.

Here’s what i have:




array(

	'name'=>'customer.first_name',

	'value'=>'$data->customer->first_name . " " . $data->customer->last_name',

	'header'=>'Customer',

)



Thanks

Oliver

@Mindphyre: thank you very much for posting back your solution. I needed exactly this and I am happy I found this post. People like you make me like Yii even more! Thank you again.

You can even put the conditions as well, for example:




array(

        'name'=>'customer.first_name',

        'value'=>'$data->customer->last_name !="" ? "$data->customer->last_name":"$data->customer->last_name" . " " . ',


        'header'=>'Customer',

)



thanks guys for sharing,

I used your idea to solve my problem as well.

This way, Yii shall always continue to be the leading php framework.

    array(


    'name'=>'rim.first_name',


    'value'=>'$data->rim->first_name . " " . $data->rim->last_name',


    'header'=>'Full Name',


     ),