Yii Tutorial
Yii Useful Resources
Selected Reading
- Gii – Generating Module
- Gii – Generating Controller
- Gii – Creating a Model
- Yii - Gii
- Yii - Localization
- Yii - Authorization
- Yii - Authentication
- Yii - Error Handling
- Yii - Logging
- Yii - Aliases
- Yii - Fragment Caching
- Yii - Caching
- Yii - Testing
- Yii - Fields
- Yii - RESTful APIs in Action
- Yii - RESTful APIs
- Yii - Theming
- Yii - Database Migration
- Yii - Active Record
- Yii - Query Builder
- Yii - Data Access Objects
- Yii - Database Access
- Yii - Dependency Injection
- Yii - Configurations
- Yii - Creating a Behavior
- Yii - Behaviors
- Yii - Creating Event
- Yii - Events
- Yii - GridView Widget
- Yii - ListView Widget
- Yii - Data Widgets
- Yii - Data Providers
- Yii - Properties
- Yii - Sorting
- Yii - Pagination
- Yii - Formatting
- Yii - Files Upload
- Yii - Using Cookies
- Yii - Cookies
- Yii - Using Flash Data
- Yii - Sessions
- Yii - AJAX Validation
- Yii - Ad Hoc Validation
- Yii - Validation
- Yii - HTML Forms
- Yii - Rules of URL
- Yii - URL Routing
- Yii - URL Formats
- Yii - Responses
- Yii - HTTP Requests
- Yii - Creating Extensions
- Yii - Extensions
- Yii - Asset Conversion
- Yii - Assets
- Yii - Layouts
- Yii - Views
- Yii - Modules
- Yii - Widgets
- Yii - Models
- Yii - Using Actions
- Yii - Using Controllers
- Yii - Controllers
- Yii - Entry Scripts
- Yii - Application Structure
- Yii - Create Page
- Yii - Installation
- Yii - Overview
- Yii - Home
Yii Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Yii - Creating a Behavior
Yii - Creating a Behavior
Assume we want to create a behavior that will uppercase the “name” property of the component the behavior is attached to.
Step 1 − Inside the components folder, create a file called UppercaseBehavior.php with the following code.
<?php namespace appcomponents; use yiiaseBehavior; use yiidbActiveRecord; class UppercaseBehavior extends Behavior { pubpc function events() { return [ ActiveRecord::EVENT_BEFORE_VALIDATE => beforeVapdate , ]; } pubpc function beforeVapdate($event) { $this->owner->name = strtoupper($this->owner->name); } } ?>
In the above code we create the UppercaseBehavior, which uppercase the name property when the “beforeVapdate” event is triggered.
Step 2 − To attach this behavior to the MyUser model, modify it this way.
<?php namespace appmodels; use appcomponentsUppercaseBehavior; use Yii; /** * This is the model class for table "user". * * @property integer $id * @property string $name * @property string $email */ class MyUser extends yiidbActiveRecord { pubpc function behaviors() { return [ // anonymous behavior, behavior class name only UppercaseBehavior::className(), ]; } /** * @inheritdoc */ pubpc static function tableName() { return user ; } /** * @inheritdoc */ pubpc function rules() { return [ [[ name , email ], string , max => 255] ]; } /** * @inheritdoc */ pubpc function attributeLabels() { return [ id => ID , name => Name , email => Email , ]; } } ?>
Now, whenever we create or update a user, its name property will be in uppercase.
Step 3 − Add an actionTestBehavior function to the SiteController.
pubpc function actionTestBehavior() { //creating a new user $model = new MyUser(); $model->name = "John"; $model->email = "john@gmail.com"; if($model->save()){ var_dump(MyUser::find()->asArray()->all()); } }
Step 4 − Type http://localhost:8080/index.php?r=site/test-behavior in the address bar you will see that the name property of your newly created MyUser model is in uppercase.
Advertisements