Behaviors are instances of the yii\base\Behavior class. A behavior injects its methods and properties to the component it is attached to. Behaviors can also respond to the events triggered by the component.
Step1Ā ā To define a behavior, extend theĀ yii\base\BehaviorĀ class.
namespace app\components; use yii\base\Behavior; class MyBehavior extends Behavior { private $_prop1; public function getProp1() { return $this->_prop1; } public function setProp1($value) { $this->_prop1 = $value; } public function myFunction() { // ... } }
The above code defines the behavior with one property (prop1) and one method (myFunction). When this behavior is attached to a component, that component will also have the prop1 property and the myFunction method.
To access the component the behavior is attached to, you may use the yii\base\Behavior::$owner property.
Step2Ā ā If you want a behavior to respond to the component events, you should override theĀ yii\base\Behavior::events()Ā method.
namespace app\components; use yii\db\ActiveRecord; use yii\base\Behavior; class MyBehavior extends Behavior { public function events() { return [ ActiveRecord::EVENT_AFTER_VALIDATE => 'afterValidate', ]; } public function afterValidate($event) { // ... } }
Step3Ā ā To attach a behavior, you should override theĀ behaviors()Ā method of the component class.
namespace app\models; use yii\db\ActiveRecord; use app\components\MyBehavior; class MyUser extends ActiveRecord { public function behaviors() { return [ // anonymous behavior, behavior class name only MyBehavior::className(), // named behavior, behavior class name only 'myBehavior2' => MyBehavior::className(), // anonymous behavior, configuration array [ 'class' => MyBehavior::className(), 'prop1' => 'value1', 'prop2' => 'value2', 'prop3' => 'value3', ], // named behavior, configuration array 'myBehavior4' => [ 'class' => MyBehavior::className(), 'prop1' => 'value1' ] ]; } }
Step4Ā ā To detach a behavior, callĀ the yii\base\Component::detachBehavior()Ā method.
$component->detachBehavior('myBehavior');
To show behaviors in action, we need data.
Preparing the DB
Step1Ā ā Create a new database. Database can be prepared in the following two ways.
- In the terminal run mysql -u root āp.
- Create a new database via CREATE DATABASE helloworld CHARACTER SET utf8 COLLATE utf8_general_ci;
Step2Ā ā Configure the database connection in theĀ config/db.phpĀ file. The following configuration is for the system used currently.
<?php return [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host = localhost;dbname = helloworld', 'username' => 'vladimir', 'password' => '12345', 'charset' => 'utf8', ]; ?>
Step3Ā ā Inside the root folderĀ run ./yii migrate/create test_table. This command will create a database migration for managing our DB. The migration file should appear in theĀ migrationsĀ folder of the project root.
Step4Ā ā Modify the migration file (m160106_163154_test_table.phpĀ in this case) this way.
<?php use yii\db\Schema; use yii\db\Migration; class m160106_163154_test_table extends Migration { public function safeUp() { $this->createTable("user", [ "id" => Schema::TYPE_PK, "name" => Schema::TYPE_STRING, "email" => Schema::TYPE_STRING, ]); $this->batchInsert("user", ["name", "email"], [ ["User1", "user1@gmail.com"], ["User2", "user2@gmail.com"], ["User3", "user3@gmail.com"], ["User4", "user4@gmail.com"], ["User5", "user5@gmail.com"], ["User6", "user6@gmail.com"], ["User7", "user7@gmail.com"], ["User8", "user8@gmail.com"], ["User9", "user9@gmail.com"], ["User10", "user10@gmail.com"], ["User11", "user11@gmail.com"], ]); } public function safeDown() { $this->dropTable('user'); } } ?>
The above migration creates a user table with these fields: id, name, and email. It also adds a few demo users.
Step5Ā āInside the project rootĀ run./yii migrateĀ to apply the migration to the database.
Step6Ā ā Now, we need to create a model for ourĀ user table. For the sake of simplicity, we are going to use theĀ GiiĀ code generation tool. Open up thisĀ url: http://localhost:8080/index.php?r=gii. Then, click the āStartā button under the āModel generatorā header. Fill in the Table Name (āuserā) and the Model Class (āMyUserā), click the āPreviewā button and finally, click the āGenerateā button.

Fantastic article post.Thanks Again. Really Cool.