Wednesday 10 April 2013

Basic Install yii

1. Download yii folder

2. Go to var/www directory in terminal
3. TO create a new project follow:

first you need to get into your webroot by

cd /var/www <– in my case

create a folder that you want your demo to be install in my case I create a folder named ‘blog’ under /var/www/yii/

then set permission to 777 by: sudo chmod 777

then type in the following command

php yii/framework/yiic.php webapp /var/www/yii/blog

if your php program has not been installed, the system will give you an install command then do it and come back to do the same.

then you can access your demo by http://localhost/yii/blog <— in my case

4. To enable gii
go to protected>config>main.php and uncomment gii module

5. To access gii generator go to http://localhost/myproj/demo/index.php?r=gii/default/login

6. To convert url mapping into path format go to config>main.php and uncomment
‘urlManager’=>array(
     ‘urlFormat’=>’path’,
      ‘rules’=>array(
       ‘/’=>’/view’,
       ‘//’=>’/’,
       ‘/’=>’/’,
      ),
),


7. Passing variable data from controller to view

In Controller
public $message = ‘Hello World rom controller’;
public function actionIndex()
{
$this->render(‘index’,array(‘content’=>$this->message));
}
Now variable content is available to modified

In View

Alternatively we could do
message   ?>
8. CRUD generation
a) Config database
b) Go to protected>>config>>main.php
c) Uncomment db componenent
d) Give the username , password, and db name
e) Comment out sqlite configuration which lie just above the mysql configuration

9. GO to gii

10. Select model generator

11. Enter table details and generate the model

12. We now have Message.php under model directory

13. Now click on CRUD generator

14. Enter Model name
15. Click generate
16. You can try it out by goign to baseurl/modelname
17. Access database content via these codes in controller
    public $message = ”;
   public function actionIndex()
   {
          $message=Message::model()->findByPK(3);
          $this->message=$message->content;
           $this->render(‘index’,array(‘content’=>$this->message));
    }

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'),
        );
    }