While browsing the documentation for behaviors, I discovered the following:
<?php
namespace app\components;
use yii\db\ActiveRecord;
use yii\base\Behavior;
class MyBehavior extends Behavior
{
// ...
public function events()
{
return [
ActiveRecord::EVENT_BEFORE_VALIDATE => 'beforeValidate',
];
}
public function beforeValidate($event)
{
// ...
}
}
?>
Does this mean that if I do not specify the events() method, which provide the map (event - handler), the event handler method will not execute, even if it has the same handler method name as the component that event is attached to?
I mean, if I provide no events(), beforeValidate($event) will not be run before the validation?
In Yii1 it was a bit different - we needed to provide no events() method, but if we provided the same methods name as the event handlers already existed in Owner - they would still run when we call parent::{handler_name}() from the Owner.
The example is the Yii1’s CTimestampBehavior: