Yii Framework Forum: Is There A 'first' And 'last' Method In Ar Like In Rails? - Yii Framework Forum

Jump to content

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

Is There A 'first' And 'last' Method In Ar Like In Rails? Rate Topic: -----

#1 User is offline   zeratool 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 20-September 12

Posted 08 October 2012 - 10:32 AM

Hello,

I'm new here and i'd like to ask if there a 'first' and 'last' method in AR like in Rails?

THanks!
0

#2 User is offline   Keith 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,600
  • Joined: 04-March 10
  • Location:UK

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.
0

#3 User is offline   Rodrigo Coelho 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 665
  • Joined: 05-August 10
  • Location:Rio de Janeiro, Brazil

Posted 08 October 2012 - 11:44 AM

You can use CActiveRecord::find and pass a CDbCriteria with condition and order.
0

#4 User is offline   filthy 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 68
  • Joined: 27-January 12

Posted 08 October 2012 - 11:56 AM

View Postzeratool, 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!


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'];

0

#5 User is offline   zeratool 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 20-September 12

Posted 17 November 2012 - 12:06 PM

View PostKeith, 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.


"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]
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