@@ -37,20 +37,26 @@ In the above, the `class` value is a string representing the fully qualified beh
...
@@ -37,20 +37,26 @@ In the above, the `class` value is a string representing the fully qualified beh
Creating your own behaviors
Creating your own behaviors
---------------------------
---------------------------
[[NEEDS UPDATING FOR Yii 2]]
To create your own behavior, you must define a class that extends [[yii\base\Behavior]].
To create your own behavior, you must define a class that implements the `IBehavior` interface. This can be accomplished by extending `CBehavior`. More specifically, you can extend `CModelBehavior` or `CActiveRecordBehavior` for behaviors to be used specifically with models or with Active Record models.
```php
```php
classMyBehaviorextendsCActiveRecordBehavior
namespaceapp\components;
useyii\base\Behavior;
classMyBehaviorextendsBehavior
{
{
}
}
```
```
To make your behavior customizable, like `AutoTimestamp`, add public properties:
To make it customizable, like [[yii\behaviors\AutoTimestamp]], add public properties:
```php
```php
classMyBehaviorextendsCActiveRecordBehavior
namespaceapp\components;
useyii\base\Behavior;
classMyBehaviorextendsBehavior
{
{
public$attr;
public$attr;
}
}
...
@@ -59,6 +65,10 @@ class MyBehavior extends CActiveRecordBehavior
...
@@ -59,6 +65,10 @@ class MyBehavior extends CActiveRecordBehavior
Now, when the behavior is used, you can set the attribute to which you'd want the behavior to be applied:
Now, when the behavior is used, you can set the attribute to which you'd want the behavior to be applied:
```php
```php
namespaceapp\models;
useyii\db\ActiveRecord;
classUserextendsActiveRecord
classUserextendsActiveRecord
{
{
// ...
// ...
...
@@ -67,23 +77,42 @@ class User extends ActiveRecord
...
@@ -67,23 +77,42 @@ class User extends ActiveRecord
{
{
return[
return[
'mybehavior'=>[
'mybehavior'=>[
'class'=>'ext\mybehavior\MyBehavior',
'class'=>'app\components\MyBehavior',
'attr'=>'member_type'
'attr'=>'member_type'
],
],
],
];
];
}
}
}
}
```
```
Behaviors are normally written to take action when certain model-related events occur, such as `beforeSave` or `afterFind`. You can write your behaviors to have the corresponding method. Within the method, you can access the model instance through `$this->getOwner()`:
Behaviors are normally written to take action when certain events occur. Below we're implementing `events` method