Site building and maintenance

Blog. Yii. Tips for Yii. Сhapter 2

Search

We continue to tell you about some interesting moments which you can meet during web development based on a Yii framework.

  1. Behaviors.

    Often while adding a new code or updating an existing one, you need to update the field in the table, for example date_updated / date_created.

    For this purpose you should add in the model:

     

    1. public function behaviors(){  
    2.     return array(  
    3.         'AutoTimestampBehavior' => array(  
    4.             'class' => 'zii.behaviors.CTimestampBehavior',  
    5.             'createAttribute' => 'date_updated',  
    6.             'updateAttribute' => 'date_updated',  
    7.         ),  
    8.     );  
    9. }  

     Now by adding or updating the code the field date_updated will be filled with actual value automatically.

  1. Images from the model into GridView.

    If you want to display images of each object in a column, write 'type' => 'image' and 'value' => 'path to the image':

     

    1. $this->widget('zii.widgets.grid.CGridView'array(  
    2.     'dataProvider'=>$model->search(),  
    3.     'filter'=>$model,  
    4.     'columns'=>array(  
    5.         array (  
    6.             'name' => 'img',  
    7.             'type' => 'image',  
    8.             'value'=> 'url/to/image' /* например: 'Yii::app()->request->baseUrl."/".$data->sliderThumbPath."/".$data->img' - only into quotes */  
    9.             'filter' => false,  
    10.         ),  
    11.         ...  
    12. ));  

     

  2. How to specify the name of the script before validation and its rules of validation:

     

    1. $model->scenario = 'upload';  
    2.       
    3. if($model->validate()) {  
    4.     ...  
    5. }  

     

    In the rules of validation we specify the scenario 'on' => 'upload':

     

    1. public function rules() {  
    2.     return array(  
    3.         array(  
    4.             'upload''file',  
    5.             'types' => "{$this->supportExt}",  
    6.             'maxSize' => $this->fileMaxSize,  
    7.             'tooLarge' => Yii::t('module_slider''The file was larger than {size}MB. Please upload a smaller file.'array('{size}' => $this->fileMaxSize)),  
    8.             'on' => 'upload',  
    9.         ),  
    10.     );  
    11. }  

     

  3. Nice extension for resizing - http://www.yiiframework.com/extension/image/

     

    1. if(isset($_POST["{$this->modelName}"])){  
    2.     $model->attributes = $_POST;                  
    3.     $model->scenario = 'upload';  
    4.       
    5.     if($model->validate()) {  
    6.         $model->upload = CUploadedFile::getInstance($model,'upload');                                 
    7.         $model->img = md5(uniqid()).'.'.$model->upload->extensionName;                
    8.           
    9.         if($model->save()){  
    10.             $model->upload->saveAs($model->uploadPath.'/'.$model->img);  
    11.               
    12.             Yii::import('application.extensions.image.Image');  
    13.             $image = new Image($model->uploadPath.'/'.$model->img);  
    14.             $image->resize($model->maxWidth, $model->maxHeight);  
    15.             $image->save();  
    16.               
    17.             Yii::app()->user->setFlash(  
    18.                     'mesSlider', tt('Image succesfullty added to slider.')  
    19.             );  
    20.             $model->unsetAttributes();                
    21.         }  
    22.     }  
    23. }  

     

    See it in details:

     

    1. Yii::import('application.extensions.image.Image'); // import extension  
    2. $image = new Image($model->uploadPath.'/'.$model->img); // as parameter specify an path to the file  
    3. $image->resize($model->maxWidth, $model->maxHeight); //parameters – max width and max height  
    4. $image->save();   

     

    Due to this extension you can write less code to resize the image and to save it.

  4. Hide of View and Update buttons in GridView. By clicking the button "Delete" - you will see javascript alert confirmation.

     

    1. array(  
    2.     'class'=>'CButtonColumn',  
    3.     'buttons' => array(  
    4.         'view' => array(  
    5.             'visible' => 'false',  
    6.         ),  
    7.         'update' => array(  
    8.             'visible' => 'false',  
    9.         ),  
    10.     ),  
    11.     'deleteConfirmation' => 'Are you sure you want to delete this item?'  
    12. )  

     

  5. Usually after any action a user expects a system notification (which indicates whether the operation succeeded or failed)..

    For example, the user expects it after he has added a new image, but he does not get it, because the script is written in such a way that the user remains on the same page (which allows him to add images) and does not go to the page (where he can see the list of images).

    To create an output of notifications which indicates whether the operation succeeded or failed you can use:

     

    1. Yii::app()->user->setFlash(  
    2.         'message''Image succesfullty added to slider.'  
    3. );  
    4. // clear data from last form  
    5. $model->unsetAttributes();  

     

    and add some more lines of code in the view:

     

    1. if (Yii::app()->user->hasFlash('message')) {  
    2.     echo "<div class='success'>" . Yii::app()->user->getFlash('message') . "</div>";  
    3. }  

     

    Now the user will get a notification that the image is uploaded.

    It takes less than a minute to develop, but the user gets a great pleasure.

  6. For example, while working with images it is very useful to get data about the location of folders with files from any file in code.

    In such a situation I write the following code:

     

    1. ...  
    2.   
    3.     $public sitePath;  
    4.     $public uploadPath;  
    5.     ...  
    6.     public function init() {  
    7.         $this->preparePaths();  
    8.     }  
    9.   
    10.     public function preparePaths() {  
    11.         $this->sitePath = Yii::app()->basePath . '/../';  
    12.         $this->uploadPath = $this->sitePath . '/uploads/slider;  
    13.         ...  
    14.     }  
    15. ...  

     

    Now you can get paths of images: $this->uploadPath, ModelName::model()->uploadPath depending on the situation.

Discuss the article in the forum