Yii Framework Forum: Limit For Has_One Relation - Yii Framework Forum

Jump to content

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

Limit For Has_One Relation Rate Topic: -----

#1 User is offline   martijnjonkers 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 07-June 13

Posted 18 October 2013 - 03:35 AM

Hi,

I got 2 tables, Devices and Locations.
Each device HAS_MANY Locations.

But i want to get the last known location via a relation inside the Devices table:

this works:
'location' => array(self::HAS_MANY, 'Location', 'DeviceID','limit'=>1,'order'=>'Time ASC')


But this gives me an array with 1 element.
I just want the location model, not an array.

I tried this:
'location' => array(self::HAS_ONE, 'Location', 'DeviceID','order'=>'Time ASC')


It works, but the sql generated gives me ALL locations and the relation only return the first one found.
When i add
'limit'=>1
I get an error:
Property "CHasOneRelation.limit" is not defined.

Why can't I use limit with HAS_ONE?

thnx!
0

#2 User is offline   Kostas Apazidis (KonApaz) 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,380
  • Joined: 21-February 11
  • Location:Greece

Posted 18 October 2013 - 04:08 AM

Hi @martijnjonkers

try this

'location' => array(self::HAS_ONE, 'Location', 'DeviceID','order'=>'Time ASC','on'=>'Location.ID=(select MAX(ID) from Location)');


OR

'location' => array(self::HAS_ONE, 'Location', 'DeviceID','order'=>'Time ASC','condition'=>'LIMIT 1');

Yii is the best php framework in the world!
It is also powerful and flexible for large scale websites
find our demo Yii extension on www.webkit.gr
Is it post useful? please v++ ;)
0

#3 User is offline   martijnjonkers 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 07-June 13

Posted 18 October 2013 - 04:36 AM

View PostKonApaz, on 18 October 2013 - 04:08 AM, said:

Hi @martijnjonkers

try this

'location' => array(self::HAS_ONE, 'Location', 'DeviceID','order'=>'Time ASC','on'=>'Location.ID=(select MAX(ID) from Location)');


OR

'location' => array(self::HAS_ONE, 'Location', 'DeviceID','order'=>'Time ASC','condition'=>'LIMIT 1');




Unfortunatly,

The sub query in the first one gives back the last ID inserted, not the last ID corresponding to the requested Device.
The second gives me syntax errors.
0

#4 User is offline   martijnjonkers 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 43
  • Joined: 07-June 13

Posted 18 October 2013 - 04:56 AM

I actually got the desired result when adding the limit to the order option:

'location' => array(self::HAS_ONE, 'Location', 'DeviceID','order'=>'Time DESC LIMIT 1'),


I know it it not supposed to be used this way, but i can't find any other solution.
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