Yii Framework Forum: Ordering A Model - Yii Framework Forum

Jump to content

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

Ordering A Model Rate Topic: -----

#1 User is offline   lup0z 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 60
  • Joined: 02-May 11

Posted 19 November 2012 - 04:29 AM

Hi,
in actionIndex I oredered elements modifying the method using CActiveDataProvider in this way:
public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('Articoli', array(
						'criteria'=>array(
										  'order'=>'id DESC' //'order'=>'data DESC'
										  ),
						));
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}


Now I have to accomplish the same ordering in actionAdmin but I have just the model class:
public function actionAdmin()
	{
		$model=new Articoli('search');
		$model->unsetAttributes();  // clear any default values
		if(isset($_GET['Articoli']))
			$model->attributes=$_GET['Articoli'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}


How can I order the model?
I thought I could use the array_reverse PHP function on GET but I don't know what's GET structure.
Anyway, is there any other way?

Thanks!
0

#2 User is offline   Giuseppe Santoro 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 16
  • Joined: 15-October 12

Posted 19 November 2012 - 04:40 AM

A way should be to add the ordering in the CActiveDataProvider produced by the function search() in your model.
0

#3 User is offline   lup0z 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 60
  • Joined: 02-May 11

Posted 19 November 2012 - 04:46 AM

My search() function in Model is:
public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('titolo',$this->titolo,true);
		$criteria->compare('testo',$this->testo,true);
		$criteria->compare('data',$this->data,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));


You mean I should modify it like this?
public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('titolo',$this->titolo,true);
		$criteria->compare('testo',$this->testo,true);
		$criteria->compare('data',$this->data,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
                        'criteria'=>array(
                                    'order'=>'id DESC'
                                    ),
		          ));




Or can I just remove the 'criteria'=>$criteria and adding mine?
Or maybe using the "order" property :P
0

#4 User is offline   lup0z 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 60
  • Joined: 02-May 11

Posted 19 November 2012 - 05:00 AM

Ok,
I succeed adding a

$criteria->order = 'id DESC';


in search(). Thank you so much!
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