Yii Framework Forum: beforeSave not being called - Yii Framework Forum

Jump to content

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

beforeSave not being called Rate Topic: -----

#1 User is offline   TAQTICA 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 13-February 10

Posted 16 February 2010 - 07:09 AM

Hi,
I addedd the protected function beforeSave to a model class in order to fill some fields that are not user input.
The Controller is calling the 'save' method but beforeSave is not being called. I even tried to put an exception as the first line of the beforeSave function but nothing happens.
Thanks in advance.
0

#2 User is offline   andy_s 

  • Random Member Title
  • Yii
  • Group: Moderators
  • Posts: 1,526
  • Joined: 22-June 09
  • Location:Russia, Kostroma

Posted 16 February 2010 - 07:31 AM

Can you show a code of your model & controller? beforeSave() is always called when a record is being inserted/updated.
0

#3 User is offline   Junior - df9 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 416
  • Joined: 24-May 09
  • Location:Brazil

Posted 16 February 2010 - 07:39 AM

Quote

...I addedd the protected function...


But in Yii's cookbook there is something like

public function beforeSave() {
    if ($this->isNewRecord)
        $this->created = new CDbExpression('NOW()');
    else
        $this->modified = new CDbExpression('NOW()');
 
    return parent::beforeSave();
}


Note that the function is declared as public and there is a call to the parent's beforeSave() method also

This is working around here, have you tried this way?

regards
______________________________________
Junior
df9.com.br
Linux Registered User #364954
GNU/Linux: together we're ready!
0

#4 User is offline   andy_s 

  • Random Member Title
  • Yii
  • Group: Moderators
  • Posts: 1,526
  • Joined: 22-June 09
  • Location:Russia, Kostroma

Posted 16 February 2010 - 07:46 AM

View Postscoob.junior, on 16 February 2010 - 07:39 AM, said:

But in Yii's cookbook there is something like

public function beforeSave() {
    if ($this->isNewRecord)
        $this->created = new CDbExpression('NOW()');
    else
        $this->modified = new CDbExpression('NOW()');
 
    return parent::beforeSave();
}


Note that the function is declared as public and there is a call to the parent's beforeSave() method also

This is working around here, have you tried this way?

regards


I always declare all after/before functions as "protected", because they are protected in the parent class and should not be called outside it (or any descendant).
0

#5 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 752
  • Joined: 02-February 09

Posted 16 February 2010 - 08:39 AM

View PostTAQTICA, on 16 February 2010 - 07:09 AM, said:

Hi,
I addedd the protected function beforeSave to a model class in order to fill some fields that are not user input.
The Controller is calling the 'save' method but beforeSave is not being called. I even tried to put an exception as the first line of the beforeSave function but nothing happens.
Thanks in advance.


Did you return true at the end of your function?
0

#6 User is offline   andy_s 

  • Random Member Title
  • Yii
  • Group: Moderators
  • Posts: 1,526
  • Joined: 22-June 09
  • Location:Russia, Kostroma

Posted 16 February 2010 - 08:47 AM

View Postbettor, on 16 February 2010 - 08:39 AM, said:

Did you return true at the end of your function?


I don't understand why people always say to return true in the end of "before" methods. If you'll return true, an event "onBefore..." won't be called! You should always put "return parent::beforeSave()" in the end of redefined beforeSave() method (same for other "before" methods, and without "return" for "after" methods).

And I don't think it's the problem there, since man said he put Exception at the first line...
Sorry if it is "offtop".
0

#7 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 752
  • Joined: 02-February 09

Posted 16 February 2010 - 09:52 AM

View Postandy_s, on 16 February 2010 - 08:47 AM, said:

I don't understand why people always say to return true in the end of "before" methods. If you'll return true, an event "onBefore..." won't be called! You should always put "return parent::beforeSave()" in the end of redefined beforeSave() method (same for other "before" methods, and without "return" for "after" methods).

And I don't think it's the problem there, since man said he put Exception at the first line...
Sorry if it is "offtop".


True. I was not sure if the user has included "return parent::beforeSave()" as the user did not show any code. Sorry if my comment does not help
0

#8 User is offline   TAQTICA 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 13-February 10

Posted 16 February 2010 - 09:56 PM

Here is the code:

protected function beforeSave()
{
if(parent::beforeSave())
{
if($this->isNewRecord)
{
$this->create_time=time();
}
return true;
}
else
return false;
}

THe controller function is the following:

public function actionCreate()
{
$model=new Group;
if(isset($_POST['Group']))
{
$model->attributes=$_POST['Group'];
if(!$model->save())
throw new CHttpException(400,Yii::t('base','Save failed.'));
$this->redirect(array('view','id'=>$model->id));
}
}

The funny thing is that if add the following line:

throw new CHttpException(400,'Hello');

as the first line in the beforeSave function, it is never called (so beforeSave is never called).

If I add the following line:

$model->create_time=time();

just before calling the save function in the controller, then the exception is thrown (so beforeSave is called).

I find no logical explanation for this behavior.
0

#9 User is offline   Onman 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 175
  • Joined: 26-December 09
  • Location:The Netherlands

Posted 17 February 2010 - 03:37 AM

Your code looks alright.
Do you have any rule set on the create_time attribute? (maybe post your rules() method, too.

b.t.w. if you enter code, please use the '[ code]' and '[ / code]' tags (without the spaces in it), that way it will be displayed nicely.
0

#10 User is offline   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,651
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 17 February 2010 - 05:13 AM

If validation doen't pass you would not reach the (protected) beforeSave()method. BTW remember to call parent or the onBeforeSave event would not fire.

There is also a (public) beforeSave() event handler, e.g in CActiveRecordBehavior.

/Tommy
Don't forget to read The Definitive Guide to Yii (en) (sv) | The class reference has the details
0

#11 User is offline   TAQTICA 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 13-February 10

Posted 17 February 2010 - 06:05 AM

I removed create_time from the required rule. Now it works. Validation was not passing.
Thanks to everybody.
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