samilo
(Samiloxphp)
April 6, 2013, 12:57pm
1
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
mbi
(mbi)
April 6, 2013, 1:04pm
2
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)); ?>
samilo
(Samiloxphp)
April 6, 2013, 1:22pm
3
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)); ?
samilo
(Samiloxphp)
April 6, 2013, 2:02pm
5
Thanks mbi and seenivasan .