Site building and maintenance

Blog. Yii. Advice on Yii. Сhapter 5

Search

Advice on Yii. Сhapter 5.

  1. An example for use of CArrayDataProvider.

    In some cases it is necessary to display the content of the array in CGridView.

    CArrayDataProvider will help you to do it.

    Сontroller:

    1. public function actionViewGrid() {    
    2.     $items = array(    
    3.             array('id' => 1, 'title'=>'Title1''description' => 'Description1'),    
    4.             array('id' => 2, 'title'=>'Title2''description' => 'Description2'),    
    5.             array('id' => 3, 'title'=>'Title3''description' => 'Description3'),    
    6.             array('id' => 4, 'title'=>'Title4''description' => 'Description4')    
    7.     );    
    8.         
    9.     $itemsProvider = new CArrayDataProvider($itemsarray(    
    10.         'pagination' => array(    
    11.             'pageSize' => 2,    
    12.         ),    
    13.     ));    
    14.         
    15.     $this->render('viewgrid'array('itemsProvider' => $itemsProvider));    
    16. }  

    View:

    1. $this->widget('zii.widgets.grid.CGridView'array(    
    2.    'id' => 'itemGrid',    
    3.    'dataProvider' => $itemsProvider,    
    4.    'enablePagination' => true,    
    5.    'columns' => array(    
    6.        array(    
    7.            'name' => 'ID',    
    8.            'value' => '$data["id"]',    
    9.            'sortable' => true,    
    10.            'filter' => false,    
    11.        ),    
    12.        array(    
    13.            'name' => 'Name',    
    14.            'value' => '$data["title"]'    
    15.        ),    
    16.        array(    
    17.            'name' => 'Description',    
    18.            'value' => '$data["description"]'    
    19.        ),    
    20.    )    
    21. ));  

    As a result we will get a usual GridView with an ability for pagination:

    yii-tips-5-01

    yii-tips-5-02

    Remember that each array should have an id key with a value. Otherwise, it will not work.

     

  2. A couple of useful methods.

    In Yii use Yii::app()->session->sessionId instead of session_id() to get the id of a user's session.

    Use Yii::app()->request->userHostAddress instead of $_SERVER['REMOTE_ADDR'] to get IP address of a user.

     

  3. CDbExpression.

    It is doubtless that in SQL queries many of us use built-in functions NOW(), DATE_FORMAT() etc.

    If you just add NOW() inte the query, the result will differ from the one you have expected.

    It happens because NOW() is interpreted as a line.

    1. Yii::app()->db->createCommand()->insert('table'array(      
    2.     'title'=> 'title1',      
    3.     'description'=> 'description1',    
    4.     'date_now' => 'NOW()',    
    5. ));   

    So you need to use CDbExpression:

    1. Yii::app()->db->createCommand()->insert('table'array(      
    2.     'title'=> 'title1',      
    3.     'description'=> 'description1',    
    4.     'date_now' => new CDbExpression('NOW()'),    
    5. ));   

    One more example, but now with DATE_FORMAT:

    1. $dateNow = new CDbExpression("DATE_FORMAT(date_now, '%Y-%m-%d') AS dateNowFormat");    
    2.     
    3. $allRecords = Yii::app()->db->createCommand()      
    4.     ->select('title, description, '.$dateNow.'')     
    5.     ->from('table')       
    6.     ->queryAll();   

    or:

    1. $allRecords2 = Yii::app()->db->createCommand()      
    2.     ->select(array('title''description'new CDbExpression("DATE_FORMAT(date_now, '%Y-%m-%d') AS dateNowFormat")))     
    3.     ->from('table')       
    4.     ->queryAll();   

    or:

    1. $criteria = new CDbCriteria();      
    2. $criteria->select = new CDbExpression("DATE_FORMAT(date_now, '%Y-%m-%d') AS dateNowFormat");    

     

Discuss the article in the forum