Wednesday 18 April 2012

How to remove pagination yii framework?
==============================================================
put your below code in views/admin.php file

widget('zii.widgets.CListView', array(
        'dataProvider'=>$dataProvider,
        'itemView'=>'_viewEvents',
        'summaryText'=> '', // to remove the
        'template'=>'{sorter}{pager}{summary}{items}{pager}{summary}{sorter}', // this code display pager, item, sorter etc
        'template'=>'{items}', // to remove pager from the page use this code and remove above line
   
        'pager'=>array(
                'header'=>'',//text before it
                //page'=>'',
                'firstPageLabel'=>'',//overwrite firstPage lable
                'lastPageLabel'=>'',//overwrite lastPage lable
                'nextPageLabel'=>'',//overwrite nextPage lable
                'prevPageLabel'=>'',//overwrite prePage lable
        ),
    )); ?>
 How To Chang a Error Message For Yii?
 ===================================================

Open your model file then find below function and changes it

 public function rules() {
        $message ="<span class='ui-icon ui-icon-alert'></span><span class='app'>". Yii::t('app','{attribute} cannot be blank.')."</span>";
        return array(

        array('firstname, lastname, email, username, password, restaurant_name, created_at, is_active', 'required','message'=>$message),
        array('wizard_step, send_email_updates, agree_terms, is_disabled, parent_id', 'numerical', 'integerOnly'=>true),
        array('user_type', 'length', 'max'=>7),
        array('firstname, lastname', 'length', 'max'=>50),
        array('email', 'length', 'max'=>128),
        array('username, password', 'length', 'max'=>40),
        array('restaurant_name, user_roles', 'length', 'max'=>255),
        array('id_register_hear_aboutus, id_register_identify_question', 'length', 'max'=>11),
        array('lognum', 'length', 'max'=>5),
        array('is_active', 'length', 'max'=>8),
        array('restaurant_desc, updated_at, logdate, extra', 'safe'),
        array('user_type, wizard_step, restaurant_desc, send_email_updates, agree_terms, id_register_hear_aboutus, id_register_identify_question, updated_at, logdate, lognum, user_roles, extra, is_disabled, parent_id', 'default', 'setOnEmpty' => true, 'value' => null),
        array('id, user_type, firstname, lastname, email, username, password, wizard_step, restaurant_name, restaurant_desc, send_email_updates, agree_terms, id_register_hear_aboutus, id_register_identify_question, created_at, updated_at, logdate, lognum, is_active, user_roles, extra, is_disabled, parent_id', 'safe', 'on'=>'search'),
        );
    }
How to change password for Yii?
=====================================================

Create the new action in you controller file :

public function actionChange($id)
    {
       
        $model=User::model()->findByPk((int)$id);
        $modelForm=new ChangePasswordForm();
        if(isset($_POST['ChangePasswordForm']['password']))
        {
            //print_r($_POST['ChangePasswordForm']['password']);
            if((count(CJSON::decode(CActiveForm::validate($modelForm)))>0)) {
                $this->render('change',array(
                        'model'=>$modelForm,
                ));
                Yii::app()->end();

            }

            $modelForm->password = $_POST['ChangePasswordForm']['password'];
            /*
                if(!$modelForm->validate()) {
                $this->render('change',array('model'=>$modelForm,));
                }
                */
               //$tmp=array('password'=>Yii::app()->getModule('admin')->encrypting($modelForm->password));
            //(($modelForm->password,0);
            $model->password=md5($modelForm->password);
           
            $model->save();
            if (!$model->hasErrors()) {
                echo "Password Change Succesfully";
                     $this->redirect(array('view','id'=>$id));
            }else {
               echo "Password change failure!";
                $this->redirect(array('view','id'=>$id));
            }
            //p($model->getErrors());
        }
        $this->render('change',array(
                'model'=>$modelForm,
        ));
    }
=========================================================================================================   
   
How to add a date without form post submit  in database?
=============================================
First Method :
=>put  below code in your model file 
 
public function beforeSave() {
    if ($this->isNewRecord)
        $this->created = new CDbExpression('NOW()');

    $this->modified = new CDbExpression('NOW()');

    return parent::beforeSave();
}

Second Method:

1)first upon model base file copy the below line:


    public function rules() {
         //in function rules
        array('created_at, updated_at', 'default', 'setOnEmpty' => true, 'value' => null),
    }
    public function attributeLabels() {
       //in attributesLabels function
        'created_at' => Yii::t('app', 'Created At'),
        'updated_at' => Yii::t('app', 'Updated At'),
    }
  
    public function search() {
         //in serch function
        $criteria->compare('created_at', $this->created_at, true);
        $criteria->compare('updated_at', $this->updated_at, true);
    }
  
2)In controller file :

    public action Create(){
            if (isset($_POST['User'])) {
                $tmp=array('created_at'=>date("Y-m-d H:i:s"));
                $_POST['User']=array_merge($_POST['User'],$tmp);
        }  
    }
  
    public actionUpdate(){
            if (isset($_POST['User'])) {
                $tmp=array('updated_at'=>date("Y-m-d H:i:s"));
                    if(isset($model->attributes))
                    {  
                        $g=$model->created_at;
                        //print_r($g); die;
                        $tm=explode("/",$g);
                        //print_r($tm); die;
                        $y=$tm['2'];
                        $m=$tm['1'];
                        $d=$tm['0'];
                    }  
                    $fy=$m."-".$d."-".$y;
                    //print_r($fy); die;
                    $model->created_at    =    date("Y-m-d ",strtotime($fy));
                    //print_r($model->created_at); die;
                    $model->updated_at    =    1;
                    $_POST['User']=array_merge($_POST['User'],$tmp);
                    $model->setAttributes($_POST['User']);
                    }  
    }
  
3) then put your function in model file(Whatever using Model):


        public function afterFind()
            {
                $this->created_at = strtotime($this->created_at);
                $this->created_at = date('m/d/Y', $this->created_at);
              
                $this->updated_at = strtotime($this->updated_at);
                $this->updated_at = date('m/d/Y', $this->updated_at);

                parent::afterFind ();
            }
   


Yii validation  Form in Model
===============================================
Go to your  model file and find function

 public function rules() {
        return array(
        array('firstname, lastname, email, username,password,address1,phone, mobile,is_active','required'),
        array('firstname, lastname, email, username, password, address1, address2', 'length', 'max'=>255),
        array('email','email'),
        array('phone, mobile', 'length', 'max'=>11),
        array('mobile', 'numerical', 'integerOnly'=>true),
        array('phone', 'numerical', 'integerOnly'=>true),
        array('is_active', 'length', 'max'=>8),
        array('created_at, updated_at', 'default', 'setOnEmpty' => true, 'value' => null),
        array('r_password', 'compare','compareAttribute'=>'password',),
        array('id, firstname, lastname, email, username, password, address1, address2, phone, mobile, extra, is_active', 'safe', 'on'=>'search'),
        );
    }