Hello,
I'm new here and i'd like to ask if there a 'first' and 'last' method in AR like in Rails?
THanks!
Page 1 of 1
Is There A 'first' And 'last' Method In Ar Like In Rails?
#2
Posted 08 October 2012 - 10:59 AM
There's no direct equivalent. What are you looking to use them for?
In the case of first(), just doing a find based method (rather than findAll based) will return only the first matching result, or null if no results were found. There's no inbuilt way to replicate the last() method.
In the case of first(), just doing a find based method (rather than findAll based) will return only the first matching result, or null if no results were found. There's no inbuilt way to replicate the last() method.
#4
Posted 08 October 2012 - 11:56 AM
zeratool, on 08 October 2012 - 10:32 AM, said:
Hello,
I'm new here and i'd like to ask if there a 'first' and 'last' method in AR like in Rails?
THanks!
I'm new here and i'd like to ask if there a 'first' and 'last' method in AR like in Rails?
THanks!
select max(id) id being the pk
$model = new Model; $criteria=new CDbCriteria; $criteria->select='max(column) AS maxColumn'; $row = $model->model()->find($criteria); $somevariable = $row['maxColumn'];
#5
Posted 17 November 2012 - 12:06 PM
Keith, on 08 October 2012 - 10:59 AM, said:
There's no direct equivalent. What are you looking to use them for?
In the case of first(), just doing a find based method (rather than findAll based) will return only the first matching result, or null if no results were found. There's no inbuilt way to replicate the last() method.
In the case of first(), just doing a find based method (rather than findAll based) will return only the first matching result, or null if no results were found. There's no inbuilt way to replicate the last() method.
"What are you looking to use them for?"
Since the result is an array, in rails if you use .first will return the $arr[0] element while .last will return $arr[$arr.length - 1]
Share this topic:
Page 1 of 1