Yii Framework Forum: beforesave() + getting max value from a table - Yii Framework Forum

Jump to content

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

beforesave() + getting max value from a table Rate Topic: -----

#1 User is offline   vimanaboy 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 16-March 11
  • Location:Puławy/Poland

Posted 03 May 2011 - 06:43 PM

Hi there,

i'm struggling to get my beforesave working this way:

when the user sends the form, beforesave() should fetch the highest number in a column called 'order' and insert a value of order+1 into current 'order' field.

After a few hours spent here reading posts i managed to compile this thing:

   protected function beforeSave()
        {
            if(parent::beforeSave())
            {
                if($this->isNewRecord)
                {
                      
                       $criteria->select='max(order) as myMaxOrder';
                                                                  
                       $get_it= new CActiveDataProvider(get_class($this), array(
                        'criteria'=>$criteria,
                        ));

                       $got_it=$get_it->getData();
                       
                       $whatweneed=$got_it[0]['myMaxOrder'];
                       

                       $this->order=(int)$whatweneed+1;
                     

                }

                return true;
            }
            else
            return false;
        }


It gets the MAX from 'order' but i really did't know how to deal properly with the getData() method, so i var_dumped it and saw that what i was looking was there but i still don't know how to access this value apart from doing

$whatweneed=$got_it[0]['myMaxOrder'];


Could you tell me how to do it right?
0

#2 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,991
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 04 May 2011 - 02:31 AM

Uhm, why so complicated? Something like this should do it:

function beforeSave()
{
    if (parent::beforeSave() && $this->isNewRecord) {
        $this->order=$this->dbConnection->createCommand('SELECT MAX(order)+1 FROM ..tablename here...')->queryScalar();
        return true;
}

0

#3 User is offline   vimanaboy 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 16-March 11
  • Location:Puławy/Poland

Posted 04 May 2011 - 07:14 AM

Hi Mike!

I saw your posts helping people all around including me, thank you for helping me with this thing!

Cheers :)


View PostMike, on 04 May 2011 - 02:31 AM, said:

Uhm, why so complicated? Something like this should do it:

function beforeSave()
{
    if (parent::beforeSave() && $this->isNewRecord) {
        $this->order=$this->dbConnection->createCommand('SELECT MAX(order)+1 FROM ..tablename here...')->queryScalar();
        return true;
}


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