How To Use Concat In Listdata()

Hi All

I need to print drowpdownlist with data from model so I used listdata() , but in string value i need to use concat to print full user name from model , i wrote this code to do that .

model :


 	


public function trainer_list(){

   	

    	return CHtml::listData(adminusers::model()->findAll('per=3 or per=5',array('select' => 'concat(fname_ar, " ",lname_ar) as user_name')),'user_id','user_name');

  

	}



View :


<div class="row">

		<?php echo $form->labelEx($model,'user_id'); ?>

		<?php echo chtml::activeDropDownList($model,'user_id',adminusers::model()->trainer_list(),array('prompt'=>'Select admin ')); ?>

		<?php echo $form->error($model,'user_id'); ?>

	</div>

But Results is blank in string value ? !

How to fix it ?

Thanks in advance

create a method getUsername() in your model




<?php echo chtml::activeDropDownList($model,'user_id', CHtml::listData(User::model()->findAll(), 'id', 'username'), array('prompt' => 'Select User)); ?>



Hi mbi

But i did that in my code , I guess I didn’t understood your answer in clear way .

Can you explain please ?

Thanks in advance

Dear Samilo

You have to declare a method like this in the Model.




public function getUsername()

{

return $this->fname_ar." ".$this->lname_ar;

}



Or you can directly use anonymous function in CHtml::listData




<?php echo chtml::activeDropDownList($model,'user_id', CHtml::listData(User::model()->findAll(), 'id',function($model){return $model->fname_ar." " ".$model->lname_ar;}), array('prompt' => 'Select User)); ?



Thanks mbi and seenivasan .