Populate fields from model in ascending order

I want to populate fields from model in ascending order.

Any tips?

I have gone through the documentations but was not able to get any solution.

order documentation

like this

order(array(‘PsmsDistrictMaster.districtname’, ‘id asc’)

Sorry but where to place it??


$criteria= new CDbcriteria;

$criteria->order= 'PsmsDistrictMaster.districtname ASC';

Where exactly should i use this code,because after adding it like this


public function search()

	{

		$criteria=new CDbCriteria;


                $criteria->order= 'PsmsStateMaster.statename ASC';




		$criteria->compare('stateid',$this->stateid);

		$criteria->compare('countryid',$this->countryid);

		$criteria->compare('statecode',$this->statecode,true);

		$criteria->compare('statename',$this->statename,true);

		$criteria->compare('statusflag',$this->statusflag,true);

		$criteria->compare('creationdate',$this->creationdate,true);

		$criteria->compare('modifieddate',$this->modifieddate,true);


		return new CActiveDataProvider(get_class($this), array(

			'criteria'=>$criteria,

		));

	}

this is not bringing any change…

When you create a CActiveDataProvider in search() method of the model, you can set the default order by setting the ‘sort’ property like this:




public function search()

	{

		$criteria=new CDbCriteria;

-               $criteria->order= 'PsmsStateMaster.statename ASC';

		$criteria->compare('stateid',$this->stateid);

		$criteria->compare('countryid',$this->countryid);

		$criteria->compare('statecode',$this->statecode,true);

		$criteria->compare('statename',$this->statename,true);

		$criteria->compare('statusflag',$this->statusflag,true);

		$criteria->compare('creationdate',$this->creationdate,true);

		$criteria->compare('modifieddate',$this->modifieddate,true);


		return new CActiveDataProvider(get_class($this), array(

			'criteria'=>$criteria,

+			'sort' => array(

+				'defaultOrder' => 'statename ASC',

+			),

		));

	}



I want to do ordering inside a dropdown in a form

So should i add this lines to


$criteria= new CDbcriteria;

$criteria->order= 'PsmsDistrictMaster.districtname ASC';

i was not sure where to use this lines… so ended up using it in function search()

I made use of this sample code in wiki…


// retrieve the models from db

$models = categories::model()->findAll(

                 array('order' => 'category_name'));

 

// format models as $key=>$value with listData

$list = CHtml::listData($models, 

                'category_id', 'category_name');

Now everything is solved

Now i am havin a prob in using this code in dependent dropdownbox

i populate dependent dropdown using a function inside controller…


public function actionDynamicDistrict()

    {    

        $state = $_POST['PsmsEmpInfo']['EMP_STA_ID'];

		$data=PmDistrict::model()->findAll(array('order' => 'DIS_NAME'),'t.DIS_STA_ID=:DIS_STA_ID',array(':DIS_STA_ID'=> $state));

		


        $data=CHtml::listData($data,'DIS_ID','DIS_NAME');

        foreach($data as $id=>&$value)  {

			

           echo CHtml::tag('option', array('value'=>$id),CHtml::encode($value),true);

        }

		

		

    }

Inside view


<div class="column">

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

		<?php echo $form->dropdownlist($model,'EMP_STA_ID',CHtml::listData(PmState::model()->findAll(),'STA_ID', 'STA_NAME'),

							array(

                                'prompt'=>'Select a State',

                                'ajax'=> array( 

                                'type' => 'POST',

                                'url'=>CController::createUrl('PtEmpInfo/dynamicdistrict'),

                                'update'=>'#'.CHtml::activeId($model,'EMP_DIS_ID'),

                                )    

                        ) 

				); ?>

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

</div>


<div class="column">

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

		<?php echo $form->dropdownlist($model,'EMP_DIS_ID',array('id'=>'Select a District')); ?>

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

	</div>




The problem is i am able to load the drop-down box in ascending order. but it is not checking for the condition now.findAll(array(‘order’ => ‘VS_TYPE’),‘VS_TYPE=“GENDER”’),




<div class="column">

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

		<?php echo$form->dropdownlist($model,'ORD_GENDER_ID',CHtml::listData(PmValueSet::model()->

findAll(array('order' => 'VS_TYPE'),'VS_TYPE="GENDER"'), 'VS_ID', 'VS_VALUE')); ?>

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

	</div>