Site building and maintenance

Blog. Yii. Tips for Yii. Сhapter 4

Search

Tips for Yii. Сhapter 4.

  1. Label marked as a required field.

    Sometimes it is necessary to mark a label as a required field (marked with an asterisk), but you may not write 'required' in the rules.

    So you should pass 'required' => true to the htmlOption array:

    1. echo CHtml::activeLabel($model'name'array('required' => true));  

     

  2. CGridView. How to add a drop-down list for sorting/search..

    The filter in CGridView which is set to true is a text field by default.

    Let us see how to turn it to a drop-down list (dropDownList).

    Add the following code to its view:

    1. 'filter' => array(1 => 'Sale', 2 => 'Rent'),  

    So you will have the following:

    1. $this->widget('bootstrap.widgets.TbGridView'array(      
    2.     'id'=>'apartments-grid',      
    3.     'dataProvider'=>$model->search(),      
    4.     'filter'=>$model,      
    5.     'columns'=>array(      
    6.         ...      
    7.         array(      
    8.             'name' => 'type',      
    9.             'type' => 'raw',      
    10.             'value' => 'Apartment::getNameByType($data->type)',      
    11.             'filter' => array(1 => 'Sale', 2 => 'Rent'),    
    12.         ),            
    13.         ...      
    14.     )      
    15. ));    

    And now you need only to add the following code to a search method of the model:

    1. if ($this->type)    
    2.     $criteria->compare('type'$this->type);   

    As a result you will have the following filter drop-down list:

    yii-tips-4-01

     

  3. Query caching.

    When a query is cached, its result is stored in the query cache and is taken from it when needed.

    For instance, if you have to store the cache in the file, you need to write in config/main.php file the following:

    1. 'cache'=>array(    
    2.     'class'=>'system.caching.CFileCache',    
    3. ),  

    Set the cache time to 1000 seconds for the DAO:

    1. $sql = 'SELECT field1, field2 FROM {{apartment_reference}}';    
    2. $dependency = new CDbCacheDependency('SELECT MAX(date_updated) FROM apartment_reference');    
    3. $apReference = Yii::app()->db->cache(1000, $dependency)->createCommand($sql)->queryAll();    

    Do all the same for AR:

    1. $dependency = new CDbCacheDependency('SELECT MAX(date_updated) FROM apartment_reference');        
    2. $apReference = apReference::model()->cache(1000, $dependency)->findAll();  

    If you would like to clear the entire cache, call the flush() method:

    1. Yii::app()->cache->flush();   

     

  4. Debugging SQL queries.

    In my opinion the most qualitative debugger for Yii is yii-debug-toolbar extension.

    If you do not have an opportunity/necessity/wish to install it, but you have to fix smth, for example, drop of sql query, you can get help from logging in FireBug/Google Chrome Developer Tools.

    For this purpose, you should make the following changes in the config/main.php file:

    1. ...    
    2. 'db'=>array(    
    3.     ...    
    4.     'charset' => 'utf8',    
    5.     'enableParamLogging' => 0,    
    6.     'enableProfiling' => true,    
    7.     'schemaCachingDuration' => 0,    
    8.     ....    
    9. ),    
    10.     
    11. 'log'=>array(    
    12.     'class'=>'CLogRouter',    
    13.     'routes'=>array(    
    14.         array(    
    15.             'class'=>'CWebLogRoute',    
    16.             'levels'=>'trace',    
    17.             'showInFireBug'=>true,    
    18.         ),    
    19.     ),    
    20. ),    
    21. ...   

    Uncomment the following lines in the index.php file:

    defined('YII_DEBUG') or define('YII_DEBUG',true); and defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);

    1. // remove the following lines when in production mode    
    2. defined('YII_DEBUG'or define('YII_DEBUG',true);    
    3. // specify how many levels of call stack should be shown in each log message    
    4. defined('YII_TRACE_LEVEL'or define('YII_TRACE_LEVEL',3);   

    The "Console" tab in FireFox:

    yii-tips-4-02

    The "Console" tab in Google Chrome:

    yii-tips-4-03

    For example, we have the query:

    1. $sql = 'DELETE FROM {{apartment_reference}} WHERE apartment_id="'.$this->id.'"';  

    By calling getText() which allows to view the query, not to execute it.

    1. var_dump(Yii::app()->db->createCommand($sql)->text);    

    As a result we get the following:

    1. string(62) "DELETE FROM re_dev_apartment_reference WHERE apartment_id="37""  

     

Discuss the article in the forum