Yii Model Tutorials

Insert, Update And Change Password In Yii Model

This tutorial will help you to understand “how to handle password field in yii”. I used scenario concept to change password. So the rule of models was configured based on scenario concept of yii framework.It was working fine for me.In Model.php Rules public function rules() { return array( array('username, password,confirmpassword', 'required', 'on'=>'insert'), array('username, password,confirmpassword', 'required', 'on'=>'updateuser'), array('password,confirmpassword', 'required', 'on'=>'changepassword'), array('password', 'compare', 'compareAttribute' =>'confirmpassword', 'on'=>'insert,changepassword'), array('password','passwordalphanumeric', 'on'=>'changepassword'), } public function passwordalphanumeric($attribute_name,$params){ […]... Read More »

Username Validation In Yii Model

From my sourcecode, I gave the information for username validation in yii framework.<?php class User extends CActiveRecord { public static function model($className=__CLASS__) { return parent::model($className); } public function tableName() { return 'tbl_user'; } public function rules() { array('username', 'match', 'pattern' => '/^[A-Za-z0-9_]+$/u', 'message'=>'Username can contain only alphanumeric characters and hyphens(-).' ), array('username','unique'), } } ?>... Read More »

Email Validation In Yii Model

From my sourcecode, I gave the information for email validation in yii framework. <?php class User extends CActiveRecord { public static function model($className=__CLASS__) { return parent::model($className); } public function tableName() { return 'tbl_user'; } public function rules() { array('email','email'), array('email','unique'), //option } } ?>... Read More »

Password Validation In Yii Model

From my sourcecode, I gave the information for password validation in yii framework. I used password and confirm password validation. <?php class User extends CActiveRecord { public static function model($className=__CLASS__) { return parent::model($className); } public function tableName() { return 'tbl_user'; } public function rules() { array('password', 'compare', 'compareAttribute' => 'confirmpassword', 'on'=>'insert' ), } } ?>... Read More »

Trim Function in Yii Model

From my sourcecode, I gave the information for trim function in yii framework. When we update the record, This function will apply to all fields. <?php class User extends CActiveRecord { public static function model($className=__CLASS__) { return parent::model($className); } public function tableName() { return 'tbl_user'; } public function rules() { array('username, password, firstname, lastname, email, address', 'filter', 'filter'=>'trim'), } } ?>... Read More »

CDbTransaction In Yii

To use transaction, do like the following:Using transaction function commit and rollback, We can avoid the errors on db insert.beginTransaction With Model $transaction = Yii::app()->db->beginTransaction(); try { $model=new User; ............... ............... $model->save(); $transaction ->commit(); } catch (Exception $error) { $transaction ->rollback(); throw $error; } beginTransaction With createCommand <?php $transaction=$connection->beginTransaction(); try { $connection->createCommand($sql1)->execute(); $connection->createCommand($sql2)->execute(); //.... other SQL executions $transaction->commit(); } catch(Exception $e) { $transaction->rollback(); } ?>... Read More »

Yii Login Message

When we login in yii, we will get the error message only for “username” and “password”. Now I added to code here to set and get more error message in yii login form.UserIdentity.php<?php /** * UserIdentity represents the data needed to identity a user. * It contains the authentication method that checks if the provided * data can identity the user. */ class UserIdentity extends CUserIdentity { private $_id; const […]... Read More »

Yii Model Delete

This tutorial will help you to delete the record from database table using model in yii framework. I added the code to delete one or more record using yii framework model. /** 1 **/ $model=User::model()->findByPk($id); if($model) $model->delete(); /** 2 **/ $model=User::model()->deleteAll(); /** 3 **/ $model=User::model()->deleteAll(array("condition"=>"userid='$id'")); /** 4 **/ $mode=User::model()->deleteAll("status='A'");... Read More »

Yii Model Update

This tutorial will help you to get the information about yii update function. I added the source code to update one or multiple records using different update function in yii framework model. <?php $model=User::model()->updateByPk( $ID, array("status"=>'DECLINED') ); $model=User::model()->updateAll(array( 'first_name'=>"$firstname", 'email'=>"$email" ), "UserID=$id and status IS NULL " ); $model=User::model()->updateAll( array('status' => 1), 'UserID ='.$userid ); ?>... Read More »

Parent Child Tree Function

This tutorial will help you to create the parent child tree concept using capplication component in yii framework.I have added the source code to create this format array. Database containing following fields. categoryid|categoryname|parentid <?php class Datacomponent extends CApplicationComponent { public $allchild; public function init() { } // GET CATEGORY LIST public function getCategorylist() { $categorymodel=Category::model()->findAll(array( 'select'=>'categoryname,parentid,categoryid' )); return $categorymodel; } // PARENT FUNCTION OF PARENT CHILD TREE CONCEPT public function […]... Read More »