Yii Framework Forum: Aggregate function, How to ?! - Yii Framework Forum

Jump to content

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

Aggregate function, How to ?! Rate Topic: -----

#1 User is offline   crioz 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 17-May 09
  • Location:Estonia

Posted 04 June 2009 - 02:25 PM

I needed to get MAX valueid ID field. Problem was that "name" field appeared in the output and "maxID" did not, this is the code:

$criteria=new CDbCriteria;
$criteria->select = 'name, MAX(id) as maxID';
$last = Tree::model()->find($criteria);
echo $last->name;  // this is working !
echo $last->maxID; // error, this field does not exists ! Property "??.maxID" is not defined.

After short time of seach an answer i solved that problem:
$criteria=new CDbCriteria;
$criteria->select = 'name, MAX(id) as id';
$last = Tree::model()->find($criteria);
echo $last->id;

some how id propertie already defined and this at last is overwritten  :)
Does anybody knows what does this mean "Property "??.maxID" is not defined." ?

0

#2 User is offline   Spyros 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 461
  • Joined: 14-April 09
  • Location:Athens - Greece

Posted 04 June 2009 - 10:22 PM

In your model class you have to define a maxID variable

class myModelextends CActiveRecord {

public $maxID;
......
}

Overiding $id could be dangerous because if you later save this record you may end up with wrong data in the database
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