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!
konapaz
(Konapaz)
2
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.
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.