Yii Framework Forum: How To Use Concat In Listdata() - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How To Use Concat In Listdata() Rate Topic: -----

#1 User is offline   samilo 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 231
  • Joined: 04-October 12

Posted 06 April 2013 - 07:57 AM

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>


Posted Image

But Results is blank in string value ? !

How to fix it ?

Thanks in advance
0

#2 User is offline   mbi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 604
  • Joined: 08-May 09

Posted 06 April 2013 - 08:04 AM

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)); ?>

1

#3 User is offline   samilo 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 231
  • Joined: 04-October 12

Posted 06 April 2013 - 08:22 AM

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
0

#4 User is offline   seenivasan 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 620
  • Joined: 17-June 12
  • Location:Chennai,TamilNadu,India.

Posted 06 April 2013 - 08:40 AM

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)); ?

1

#5 User is offline   samilo 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 231
  • Joined: 04-October 12

Posted 06 April 2013 - 09:02 AM

Thanks mbi and seenivasan .
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users