Showing posts with label Yii Model. Show all posts
Showing posts with label Yii Model. Show all posts

Wednesday, October 10, 2012

Yii Model On Insert, Update And Change Password


When I wrote code for change password in yii framework, I was written above code. In this code i used scenario for model. So the rule of models was configured based on scenari concept of yii framework.It was working fine for me.
Read More...

Wednesday, July 18, 2012

Yii Model Rules

This tutorial will help you to understand the yii model rules, user defined functions. In yii rules function I added code (of yiiframework) for unique,email,password comparison, date, phone number, trim etc.. I created user functions for alphanumeric password validation, phone number or mobile number requirements validation.
<?php
class Mytable extends CActiveRecord{

public static function model($className=__CLASS__){
return parent::model($className);
}

public function rules()
{
return array(
array('status', 'numerical', 'integerOnly'=>true),
array('username, password, firstname, lastname, contactno', 'length', 'max'=>45),
array('gender, newsletter', 'length', 'max'=>1),

/** Username validation in yii model **/
array('username', 'match' ,'pattern'=>'/^[A-Za-z0-9_]+$/u',
'message'=> 'Username can contain only alphanumeric characters and hyphens(-).'),

/** Set scenario for model. Yii Scenario will help you to change dynamic validation using controller.
$model=Mytable::model()->findByPk($id); //(OR) $model = new Mytable();
$model->setScenario('updateuser'); // (OR) $model->scenario ='updateuser';
**/ 
array('username','unique','on'=>'updateuser'),


/** EMAIL VALIDATION **/
//Yii M odel Rules For Email
array('emailid', 'length', 'max'=>225),
array('emailid', 'email'),

/** PASSWORD VALIDATION **/
//Yii Model Rules For Password Confirm
array('password', 'compare', 'on'=>"confirmpassword", 'compareAttribute'=>'password'),

//Yii Model alphanumeric password validation
array('password','passwordalphanumeric','on'=>'changepassword'), 

/** DATE VALIDATION **/
//Yii Model Rules For Date Format
array('dob', 'type', 'type' =>'date', 
'message' => '{attribute}: is not a date!', 'dateFormat' => 'yyyy-MM-dd'),

/** SIMPLE PHONE NUMBER VALIDATION **/
//Yii Model Rules For Entering Mobile Or Phone Number
array('stdcode,phoneno,mobileno', 'numerical', 'integerOnly'=>true),

//Validation without STD CODE NUMBER
array('phoneno,mobileno','my_required'),
//(OR)
//Validation with STD CODE NUMBER
array('phoneno,mobileno,stdcode','my_required'),

/** TRIM DATA BEFORE SEND TO DATABASE **/
//Yii Model Rules For Trimming Data
array('username', 'filter', 'filter'=>'trim'),

/** UNIQUE VALIDATION **/
//Yii Model Rules For Unique data
array('username', 'unique'),
/** Yii Float Number VALIDATION **/
array('ratio', 'match', 'pattern'=>'/^[0-9]{1,3}(\.[0-9]{0,2})?$/'),

/** Value In Condition **/
array('status', 'in', 'range'=>array(1,2,3)),

);
}



// BeforeValidate function in yii rules
public function beforeValidate() {
       if (!$this->phoneno && !$this->mobileno) {
            $this->addError('mobileno', 'Enter Mobile Number Or Phone Number');
        }
        return parent::beforeValidate();
    }

// User defined function 
//Validation without STD CODE NUMBER
public function my_required($attribute_name,$params){
     if(empty($this->phoneno) && empty($this->mobileno)){
               $this->addError($attribute_name,
                 'Please enter Telephone number or Mobile number');
     }
}

//Validation with STD CODE NUMBER
public function my_required($attribute_name,$params){
     if(empty($this->phoneno) && empty($this->mobileno)){
             $this->addError('phoneno',
                 'Please enter Telephone number or Mobile number');
     }else if(!empty($this->phoneno) && $this->stdcode==''){
             $this->addError('stdcode','Please enter STD number');
     }
}

// Check password with alphanumeric validation
public function passwordalphanumeric($attribute_name,$params){
     if(!empty($this->password)){
          if (preg_match('~^[a-z0-9]*[0-9][a-z0-9]*$~i',$this->password)) {
                // $subject is alphanumeric and contains at least 1 number
     } else { // failed
          $this->addError($attribute_name,'Please enter password with digits');
     } 
}
}
}
?>
Read More...

Monday, July 9, 2012

Yii Update Query

Update Format1

Read More...

Friday, June 15, 2012

update query in yii


Model1: UpdateByPk


Model2: updateAll
              
Read More...

Friday, May 25, 2012

Parent Child Tree Function


Read More...

Thursday, May 17, 2012

CDbCriteria Search config in model

searchcategory is new function instead of search


Read More...

Thursday, May 10, 2012

Sql Parent Child Query

Query 1:[Update Query]
UPDATE categories
SET parent_id = (
    SELECT id FROM (SELECT id, name FROM categories) c
    WHERE c.name=categories.parent_name
)
WHERE parent_name IS NOT NULL



Query 2:[Select Query]
SELECT tbl_category.parentid FROM 
     (SELECT categoryid FROM tbl_category 
           WHERE categoryname='budgets') c,tbl_category
      WHERE c.categoryid=tbl_category.parentid


Query 3:[Select Query]
SELECT tbl_category.categoryid FROM tbl_category JOIN tbl_category c 
where c.categoryid=tbl_category.parentid and c.categoryname='states';

Query 4:[Yii Select Join Query]
        $categorymodel=Category::model()->with(array(
            'parent'=>array(
            'select'=>'categoryname',
            'joinType'=>'INNER JOIN',
            'condition'=>'parent.categoryname="'.$parentname.'"',
        ),
        ))->findAll();   
Read More...

Saturday, March 31, 2012

Yii Select Query

Select Query Different Format

Select one records on Yii model: Format1

$filetype=User::model()->find('Usertype=:usertype',array(':usertype'=>'Student')); $usermodel=User::model()->findAllByPk($userid); $usermodel=User::model()->findByPk(1); $usermodel=User::model()->findAllByPk($useridarray); $usermodel=User::model()->findAllByPk(array(2,3,10));

Select Query: Format2

$model=User::model()->findAll('usertype=:usertype',array(':usertype'=>'user'));
Read More...

Yii Model Before Save

Before Save Function in Model

beforeSave() model is a nice function in yii framework //model
When we update record in table, Some fields are changed by default like when record created, when record modified etc. Here I wrote code for this action. In Format 1, I configured separately for created date, modified date. $this->isNewRecord is return true, if it is new record. Now I affected date only If $this->isNewRecord is return false, I will affect modified date only. In Format 2, I configured both created date, modified date in beforeSave() function. In this code, created date will affected only on new record. modified date will affected on when recored created or When record modified This function reduce our workflow. Just make this function once It was very helpful.
Read More...