Site building and maintenance

Blog. Yii. Tips for Yii. Chapter 11

Search

Tips for Yii. Chapter 11.

  1. rowCssClassExpression.

    Do you need to mark a record in the GridView according to the flag that is set in the database table (for instance, whether the listing is one of the "VIP" or one of the "Special offer").

    Let's use rowCssClassExpression:

    1. $this->widget('zii.widgets.grid.CGridView'array(    
    2.         'dataProvider'=>$model->search(),    
    3.         'filter'=>$model,    
    4.         'rowCssClassExpression' => '$data->getRowCssClass()',    
    5.         ...    
    6.     )    
    7. );  

    Add the getRowCssClass() method in the model:

    1. public function getRowCssClass(){    
    2.     if($this->is_special_offer){    
    3.         return 'special_offer_tr';    
    4.     }    
    5.     if($this->is_vip){    
    6.         return 'vip_tr';    
    7.     }    
    8.     return '';    
    9. }  

    Add the following code into the file of css styles:

    1. .special_offer_tr { background-color#AAAAAA }    
    2. .vip_tr { background-color#BBBBBB }    

     

  2. Additional knowledge of CActiveRecord.

    All of us know that 'scopes' and 'with' can be applied to the model.

    You can also specify the applying of additional parameters that are sent from the form by a user.

    Perhaps, I have written it not clearly for understanding, but the example will help you to see what I mean.

    Let's write in the controller:

    1. $dataProvider = new CActiveDataProvider(      
    2.     Users::model()      
    3.         ->active()      
    4.         ->with(array('posts'))      
    5.         ->searchAction(isset($_GET['Users'])?$_GET['Users']:null),      
    6.     array(      
    7.         'criteria' => array(      
    8.             'order' => 'id DESC',      
    9.         ),      
    10.         'pagination'=>array(      
    11.             'pageSize'=>10,      
    12.             'pageVar'=>'page'      
    13.         ),      
    14.     )      
    15. );   

    , where:

    "->active()" is a named group of conditions that is set in the scopes() function:

    1. public function scopes()    
    2.     {    
    3.     return array(    
    4.         'active'=>array(    
    5.             'condition'=>'active=1',    
    6.         ),    
    7.     );    
    8. }   

    "->with(array('posts'))" - here we specify the loading of the table 'Posts' related to the model 'Users':

    1. public function relations() {    
    2.     return array(    
    3.         'posts' => array(self::HAS_MANY, 'Posts''userId'),    
    4.     );    
    5. }   

    You can also specify the applying of additional parameters for the choice of records:

    "->searchAction(isset($_GET['Users'])?$_GET['Users']:null),"

    Here is the code of the searchAction() method in a 'Users' model:

    1. public function searchAction($condParams = null) {    
    2.     if(is_null($condParams)) return $this;    
    3.     
    4.     $this->unsetAttributes();    
    5.     $this->attributes=$condParams;    
    6.     
    7.     $criteria = new CDbCriteria;    
    8.     
    9.     $criteria->compare('id'$this->id, true);    
    10.     $criteria->compare('name'$this->name, true);    
    11.     
    12.     $this->getDbCriteria()->mergeWith($criteria);    
    13.     
    14.     return $this;    
    15. }  

     

  3. onBeginRequest and onEndRequest.

    Do you need your function to work before the application will be used (site's loading)?

    Let's use onBeginRequest.

    Add the following code into the protected\config\main.php file:

    1. 'onBeginRequest'=>array('BeginRequest''onStartSite'),  

    Create a BeginRequest.php file with the following code in the protected\components folder:

    1. class BeginRequest {    
    2.     public function onStartSite() {    
    3.         // Here is a code    
    4.     }    
    5.     
    6.     public function onStopSite() {    
    7.         // Here is a code    
    8.     }    
    9. }   

    You can execute the script once a day if you set the last time of performance in Yii::app()->statePersister and the futher check of it.

    For example:

    1. class BeginRequest {    
    2.     const TIME_UPDATE = 86400;    
    3.     
    4.     public function onStartSite() {    
    5.         $data = Yii::app()->statePersister->load();    
    6.     
    7.         // perform it once a day   
    8.         if (isset($data['next_check_status'])) {    
    9.             if ($data['next_check_status'] < time()) {    
    10.                 $data['next_check_status'] = time() + self::TIME_UPDATE;    
    11.     
    12.                 $this->myFunction();    
    13.     
    14.                 Yii::app()->statePersister->save($data);    
    15.             }    
    16.         } else {    
    17.             $data['next_check_status'] = time() + self::TIME_UPDATE;    
    18.     
    19.             $this->myFunction();    
    20.     
    21.             Yii::app()->statePersister->save($data);    
    22.         }    
    23.     }    
    24.     
    25.     public function myFunction() {    
    26.         // Here is a code    
    27.     }    
    28.     
    29.     public function onStopSite() {    
    30.         // Here is a code    
    31.     }    
    32. }   

    If it is necessary to perform your function after the running of an application (after site's loading), so add the following line into the configuration file (protected\config\main.php):

    1. 'onEndRequest'=>array('BeginRequest''onStopSite'),   

     

Discuss the article in the forum