yiibjorn
(Bjorn)
February 26, 2014, 7:52pm
1
Hi,
In a lot of examples I see things like:
parent::afterValidate();
//or
return parent::beforeSave();
In methods like:
protected function beforeSave()
{
if(!$this->hasErrors())
{
// Do something.
}
return parent::beforeSave();
}
I can’t figure out why you have to call parent::afterValidate(); when you already in afterValidate()?
Could someone help me out?
hs2323
(Hs2323)
February 26, 2014, 8:15pm
2
They may have other pieces of code that need processing in the parent class. For beforeSave(), I believe that it will not allow you to save the record/s if it does not return true.
yiibjorn
(Bjorn)
February 26, 2014, 8:38pm
3
Tnx hs2323.
I think I found more info in the 3th point of the answer of this question:
php
So is it correct that when I use an extended method of (for this example) the CActiveRecord class, that I always have to call the parent method first?
JFReyes
(Jose Reyes)
February 26, 2014, 8:47pm
4
Usually yes, unless you want to completely block the parent’s default processing of the method, for example if you’re substituting your own code for it.
hs2323
(Hs2323)
February 26, 2014, 9:12pm
5
If you do block out the parent method, make sure to also return a value (usually true/false) if the parent function does.
alirz23
(Alirz23)
February 26, 2014, 9:15pm
6
I would suggest you always call the parent in some cases Yii uses these events/callbacks internally
yiibjorn
(Bjorn)
February 26, 2014, 9:44pm
7
Tnx a lot people. I will use return parent::[i]name/i from now on.