From 9edeb37e53cea7b63990a5bd19c94e49ddba9646 Mon Sep 17 00:00:00 2001 From: Larry Ullman Date: Thu, 9 Jan 2014 10:46:05 -0500 Subject: [PATCH] Edited existing content, sketched out "Creating Your Own Behavior" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Undoubtedly will need to update “Creating Your Own Behavior” to reflect changes in Yii 2. --- docs/guide/behaviors.md | 67 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 58 insertions(+), 9 deletions(-) diff --git a/docs/guide/behaviors.md b/docs/guide/behaviors.md index b425fd3..657f603 100644 --- a/docs/guide/behaviors.md +++ b/docs/guide/behaviors.md @@ -1,16 +1,16 @@ Behaviors ========= -A behavior (also knows as mixin) can be used to enhance the functionality of an existing component without modifying its -code. In particular, it can "inject" its own methods and properties into the component and make them directly accessible -via the component. It can also respond to the events triggered in the component and thus intercept the normal -code execution. Unlike PHP traits, behaviors could be attached to classes at runtime. +A behavior (also knows as *mixin*) can be used to enhance the functionality of an existing component without modifying the component's +code. In particular, a behavior can "inject" its own methods and properties into the component, making them directly accessible +via the component itslef. A behavior can also respond to events triggered in the component, thus intercepting the normal +code execution. Unlike [PHP's traits](http://www.php.net/traits), behaviors can be attached to classes at runtime. Using behaviors --------------- -Behavior can be attached to any class that extends from `Component`. In order to do so you need to implement `behaviors` -method. Yii provides `AutoTimestamp` behavior that handles updating timestamp fields on saving active record model. +A behavior can be attached to any class that extends from `Component`. In order to attach a behavior to a class, the component class must implement the `behaviors` +method. As an example, Yii provides the `AutoTimestamp` behavior for automatically updating timestamp fields when saving an Active Record model: ```php class User extends ActiveRecord @@ -32,10 +32,59 @@ class User extends ActiveRecord } ``` -In the above `class` value is a string containing fully qualified behavior class name. All the other key-values are -assigned to corresponding properties of the class. +In the above, the `class` value is a string representing the fully qualified behavior class name. All of the other key-value pairs represent corresponding public properties of the `AutoTimestamp` class, thereby customizing how the behavior functions. Creating your own behaviors --------------------------- -TBD \ No newline at end of file +[[NEEDS UPDATING FOR Yii 2]] + +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 +class MyBehavior extends CActiveRecordBehavior +{ +} +``` + +To make your behavior customizable, like `AutoTimestamp`, add public properties: + +```php +class MyBehavior extends CActiveRecordBehavior +{ + public $attr; +} +``` + +Now, when the behavior is used, you can set the attribute to which you'd want the behavior to be applied: + +```php +class User extends ActiveRecord +{ + // ... + + public function behaviors() + { + return [ + 'mybehavior' => [ + 'class' => 'ext\mybehavior\MyBehavior', + '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()`: + +```php +class MyBehavior extends CActiveRecordBehavior +{ + public $attr; + public function beforeSave() { + $model = $this->getOwner(); + // Use $model->$attr + } +} +``` \ No newline at end of file