Category Archives: Yii Model

FindAll In Yii

Yii Framework. I list out all types of find, findall conditions in yii framework. It will very helpful for us. Yii find() find() <?php $model = User::model()->find(); find() With Condition <?php $model = User::model()->find(‘userid=1 AND status=”A”‘); (OR) $model = User::model()->find(‘userid=:userId And status=:Status’, array(‘:userId’=>1,’:status’=>’A’)); find() Width Criteria <?php $criteria = new CDbCriteria; $criteria->condition=’userid=1 AND status=”A”‘; $model […]

Image Upload With Dimension Validation In Yii Model

This tutorial will help you to upload the image with image extension validation, image size validation and image dimension validation. So we can fix the width and height for image. CFileValidator is used to validate the file or image types, image maximum size, image minimum size etc. To validate the dimension, we dont have option […]

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’), } } ?>

Yii 1.0 Model Structure

By default model classes are available within the protected/models dirctory. Every class must be inherit from the CModel or subclass. Yii difines two sub classes. They are CActiveRecord, CFormModel. CActiveRecord is conntected with database table model (classes) and it represent the table structure. CFormModel is the basic model class and conntected to HTML forms. Class Extend CFormModel Structure Class Extend CActiveRecord […]

Yii createCommand Delete

Functions function delete($table, $conditions=”, $params=array()) The delete() method builds and executes a DELETE SQL statement.’$table’ is a table name of delete from.’$conditions’ and ‘$params’ is like where condition of delete statement Sample <?php $user=Yii::app()->db->createCommand() ->delete(‘tbl_user’, ‘user_id=:id’, array(‘:id’=>2)); ?> OUTPUT SQL DELETE FROM `tbl_user` WHERE user_id=:id

Yii createCommand Insert

Functions function insert($table, $columns) The insert() method builds and executes an INSERT SQL statement. Sample 1 <?php $user=Yii::app()->db->createCommand() ->insert(‘tbl_user’, array(‘username’=>’bsourcecode’,’usertype’=>1,’password’=>’hai’)); ?> OUTPUT SQL INSERT INTO `tbl_user` (`username`, `usertype`,’password’) VALUES (:username,:usertype, :password) Sample 2 <?php $name=’india’; $isactive=1; $command= Yii::app()->db->createCommand( “INSERT INTO regionmaster (`regionname`,`isactive`) VALUES (:name,:isactive)”); $command->bindValue(‘:name’, $name); $command->bindValue(‘:isactive’, $isactive); $sql_result = $command->execute(); ?> OUTPUT SQL INSERT […]

Yii createCommand queryAll

I posted here examples of createCommand Query with properties. createCommand Properties are distinct, from, group, having, join, limit, offset, params, pdoStatement, select and where. CDbCommand represents an SQL statement to execute against a database. bindParam used to bind a php variable to parameter in SQL query. bindValue is used to bind a value to and […]

Yii createCommand Update

Functions function update($table, $columns, $conditions=”, $params=array()) Sample The update() method builds and executes an UPDATE SQL statement. <?php $user=Yii::app()->db->createCommand() ->update(‘tbl_user’, array( ‘username’=>’bsourcecode’, ), ‘user_id=:id’, array(‘:id’=>1)); ?> OUTPUT SQL UPDATE `tbl_user` SET `username`=:username WHERE user_id=:id

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’, […]