Yii Framework Forum: order by problem - Yii Framework Forum

Jump to content

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

order by problem Rate Topic: -----

#1 User is offline   holala 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 74
  • Joined: 23-December 09

Posted 29 April 2010 - 08:06 AM

model employee:
public function relations()
{
return array(
	'groups' => array(self::MANY_MANY, 'Group', 'groupemployee(group_id, employee_id)'),
}


table groupemployee have 3 fileds,there are: group_id,employee_id,ordering.
I want list employees with group_id=1 and order by ordering.

here is code in another controller action,but it don't work,i don't know why,please help me.
$dataProvider=new CActiveDataProvider('Employee', array(
	'criteria'=>array(
	       'condition'=>'status = :status',
	        'params'=>array(':status'=>1),
	        'with'=>array(
			array('groups'=>array('condition'=>'group_id = 1','order'=>'ordering'))
		),
	),
        'pagination'=>array(
	        'pageSize'=>20,
	),
));


thanks a lot!
0

#2 User is offline   PoL 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 506
  • Joined: 05-November 08
  • Location:Buenos Aires, Argentina

Posted 29 April 2010 - 09:15 AM

I don't know if this work, but try this:

$dataProvider=new CActiveDataProvider('Employee', array(
        'criteria'=>array(
               'condition'=>'status = :status',
                'params'=>array(':status'=>1),
                'with'=>array(
                        array('groups'=>array('condition'=>'group_id = 1','order'=>'groupemployee.ordering')) //<<NOTE THAT LINE
                ),
        ),
        'pagination'=>array(
                'pageSize'=>20,
        ),
));


Don't say what you think, think what you say
The problem is communication! Excess of communication!
0

#3 User is offline   yoshi 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 170
  • Joined: 28-February 09
  • Location:Germany

Posted 29 April 2010 - 03:11 PM

Hi,

ehm, cause the relation is in your model employee you have to switch the key attributes - i think.

Instead of
public function relations()
{
return array(
	'groups' => array(self::MANY_MANY, 'Group', 'groupemployee(group_id, employee_id)'),
}


try
public function relations()
{
return array(
	'groups' => array(self::MANY_MANY, 'Group', 'groupemployee(employee_id,group_id)'),
}


regards
0

#4 User is offline   holala 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 74
  • Joined: 23-December 09

Posted 30 April 2010 - 01:00 AM

thanks both of you!

i changed by your way,but still don't work.

i changed code to:
$dataProvider=new CActiveDataProvider('Employee', array(
	'criteria'=>array(
		'condition'=>'status = :status',
		'params'=>array(':status'=>1),
			'join'=>'INNER JOIN groupemployee ON groupemployee.employee_id=id AND groupemployee.group_id='.$model->group_id,
			'order'=>'groupemployee.ordering',
	),
        'pagination'=>array(
		'pageSize'=>20,
	),
));


it's work.
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