Yii Framework Forum: Sort elements from the HAS_MANY relation - Yii Framework Forum

Jump to content

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

Sort elements from the HAS_MANY relation Rate Topic: -----

#1 User is offline   jeanluca 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 59
  • Joined: 22-January 11
  • Location:Bunnik, Netherlands

Posted 02 June 2011 - 06:44 AM

Hi All

Suppose I have a model Company, which HAS_MANY employees:

'employees' 				=> array(self::HAS_MANY, 'Employee', 'companyid'),


So I can do:

$company = Company::model()->findByPk(101) ;
$employees = $company->employees ; // random order


No problems so var, but now I would like to influence the order of employees. For example, I want them to be order by salary
Any suggestions ?

Cheers
Luca
0

#2 User is offline   georgebuckingham 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 62
  • Joined: 25-May 11
  • Location:London, UK

Posted 02 June 2011 - 07:42 AM

Hi Luca,

I think the easiest way would be to set up a relational query, as such:

$company = Company::model()->with(array(
'employees'=>array(
'order'=>'salary',
),
))->findByPk(101);


Hope that helps!
George

This post has been edited by georgebuckingham: 02 June 2011 - 07:43 AM

[url="http://georgebuckingham.co.uk"][/url]
1

#3 User is offline   jeanluca 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 59
  • Joined: 22-January 11
  • Location:Bunnik, Netherlands

Posted 02 June 2011 - 03:53 PM

View Postgeorgebuckingham, on 02 June 2011 - 07:42 AM, said:

Hi Luca,

I think the easiest way would be to set up a relational query, as such:

$company = Company::model()->with(array(
'employees'=>array(
'order'=>'salary',
),
))->findByPk(101);


Hope that helps!
George

ok, thanks a lot!
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