Make CSqlDataProvider in Yiiframework

You can make easily CSqlDataProvider

Step – 1

Write a function into your controller (Like below function)

  1. public function actionAllCarerConfirmed(){
  2.         $sql =’SELECT*from tbl_name’;
  3.         $countSql = ‘SELECT COUNT(*) from tbl_name’;
  4.         $count = Yii::app()->db->createCommand($countSql)->queryScalar();
  5.         $dataProvider = new CSqlDataProvider($sql, array(
  6.             ‘totalItemCount’ => $count,
  7.             ‘sort’ => array(
  8.                 ‘attributes’ => array(
  9.                     ‘column_1′,‘column_2′
  10.                 ),
  11.             ),
  12.             ‘pagination’ => array(
  13.                 ‘pageSize’ => 50,
  14.             ),
  15.         ));
  16.         // $dataProvider->getData() will return a list of arrays.
  17.         $this->render(‘allCarerConfirmed’, array(
  18.             ‘dataProvider’ => $dataProvider,
  19.         ));
  20.     }

 

Step – 2

Call CGridView Widget into your view file

  1. $this->widget(‘zii.widgets.grid.CGridView’, array(
  2.                 ‘id’ => ‘carer-response-grid’,
  3.                 ‘dataProvider’ => $dataProvider,
  4.                 ‘columns’ => array(
  5.                     ‘column_1′,
  6.                     ‘column_1′,
  7. ),
  8. ));

How to query with parent table condition in yiiframework

 

  1. $lists = ProfileInformation::model()->with(array(
  2.             ‘user’ => array(
  3.               ‘condition’ => “user.user_type = ’Care Worker’”,
  4.             )
  5.           ))->findAll();

 

Here ‘user’ is a relational key which is declare ProfileInformation model.

How to add a checkbox column at first column of datagrid / CGridView

Yii have very pretty solution for this.

We need

  1. array(
  2.  ‘class’ => ‘CCheckBoxColumn’,
  3. ‘value’=>‘Your_Value’,
  4. ‘selectableRows’ => ’50′,
  5. ),

Above array need to use inside CGridView

Example:

  1. $this->widget(‘zii.widgets.grid.CGridView’, array(
  2.                         ‘id’ => ‘customers-grid’,
  3.                         ‘dataProvider’ => $dataProvider,
  4.                         ‘itemsCssClass’ => ‘table table-striped’,
  5.                         ‘columns’ => array(
  6.                             array(
  7.                                 ‘class’ => ‘CCheckBoxColumn’,
  8.                                 ‘value’=>‘Your_Value’,
  9.                                 ‘selectableRows’ => ’50′,
  10.                             ),
  11.                             array(
  12.                                 ‘name’ => ‘user_login_id_fk’,
  13.                                 ‘header’ => ‘First Name’,
  14.                                 ‘value’ => ‘$data->userLoginIdFk->customerProfiles[0]->first_name’,
  15.                             ),
  16.                             array(
  17.                                 ‘name’ => ‘user_login_id_fk’,
  18.                                 ‘header’ => ‘Last Name’,
  19.                                 ‘value’ => ‘$data->userLoginIdFk->customerProfiles[0]->last_name’,
  20.                             ),
  21.                             array(
  22.                                 ‘name’ => ‘user_login_id_fk’,
  23.                                 ‘header’ => ‘Email’,
  24.                                 ‘value’ => ‘$data->userLoginIdFk->email’,
  25.                             ),
  26.                             array(
  27.                                 ‘name’ => ‘user_login_id_fk’,
  28.                                 ‘header’ => ‘Country’,
  29.                                 ‘value’ => ‘Country::model()->getCountryName($data->userLoginIdFk->customerProfiles[0]->country_code)’,
  30.                             ),
  31.                             array(
  32.                                 ‘name’ => ‘user_login_id_fk’,
  33.                                 ‘header’ => ‘Category’,
  34.                                 ‘value’ => ,
  35.                             ),
  36.                             array(
  37.                                 ‘name’ => ‘user_login_id_fk’,
  38.                                 ‘header’ => ‘Rating’,
  39.                                 ‘value’ => ‘$data->userLoginIdFk->customerProfiles[0]->rating’,
  40.                             ),
  41.                             array(
  42.                                 ‘class’ => ‘CButtonColumn’,
  43.                                 ‘template’ => ‘{view}’,
  44.                                 ‘buttons’ => array(
  45.                                     ‘view’ => array(
  46.                                         ‘label’ => ‘View’,
  47.                                         ‘url’ => ‘Yii::app()->createUrl(“//member/customerprofile/view/”,array(“id”=>$data->user_login_id_fk,”with”=>”userloginid”))’,
  48.                                         ‘imageUrl’ => Yii::app()->theme->baseUrl . ‘/assets/icon/view.png’,
  49.                                     ),
  50.                                     ‘update’ => array(
  51.                                         ‘label’ => ‘Update’,
  52.                                         ‘imageUrl’ => Yii::app()->theme->baseUrl . ‘/assets/icon/edit.png’,
  53.                                     ),
  54.                                 ),
  55.                             ),
  56.                         ),
  57.                     ));

I think u people enjoy it.

How to use a controller function anywhere in you project/site

Step-1:

Open main.php file. Default file path is protected > config > main.php

Add One line in import array that is  ’application.controllers.*’,

After Add that code like

  1. // autoloading model and component classes
  2.     ‘import’=>array(
  3.         ‘application.models.*’,
  4.         ‘application.components.*’,
  5.  
  6.         ‘application.controllers.*’,
  7.  
  8.         ‘application.extensions.niceditor.*’,
  9.         ‘application.extensions.multiple-file-upload.*’,
  10.         ‘application.extensions.prettyPhoto.*’,
  11.     ),

See line number 6. Now Save this file.

Now it’s ready to use.

Step-2:

Suppose you want to use a function name is “PartyBill” and controller name is “CommonController”. It is very easy to use.

The Code is :

  1. $PartyBill = CommonController::PartyBill();
  2.  
  3. echo $PartyBill;

Enjoy !  !!

 

Rajib Kumar Rakhmit

www.rajibkumar.com

Find data from database using findByPk() method in yiiframework

Hey Guys,

Suppose your model name is “CompanyUnit”. Now you want to select data from there using PrimaryKey(PK). Then you can use “findByPk()” method in yiiframe. It is very easy and simple to use.

Example:

  1. <?php 
  2. $comUnit = CompanyUnit::model()->findByPk(2); 
  3. echo $comUnit->name;
  4. ?>
Here “2″ is a PK and “CompanyUnit” is a name of model.
when “echo $comUnit->name” is executed then display name base of pk.
I think you like that.

 

[Yii Framework] Rajib Kumar CGridView, If Condition

This function change value with If Condition

  1. public function statusBarang($i)
  2.     {
  3.         if($i==1)
  4.             return “Belum diproses”;
  5.         else if($i==2)
  6.             return “Sedang diproses”;
  7.         else
  8.             return “Sudah diproses”;
  9.     }
Now use that for final result
  1. $this->widget(‘zii.widgets.grid.CGridView’, array(
  2.     ‘id’=>‘junction-tab-grid’,
  3.     ‘dataProvider’=>$model->search(),
  4.     ‘columns’=>array(
  5.         ‘id’,
  6.         ‘kodeBarang’,
  7.         array(
  8.             ‘name’=>‘Nama Mahasiswa’,
  9.             ‘type’=>‘raw’,
  10.             ‘value’=>‘Barang::model()->statusBarang($data->statusBarang)’,
  11.         ),
  12.         array(
  13.             ‘class’=>‘CButtonColumn’,
  14.         ),
  15.     ),
  16. ));
Rajib Kumar
http://www.rajibkumar.com

Easy to create dependent dropdown in yii framework

In “_form.php”

  1. <!–First Select Box–>
  2. <select class=“select” name=“SaleOrderProduct[product_category_id]“ id=“product_category_id”>
  3. <option value=“0″>- - - Select Product Category - - -</option>
  4. <option value=“1″>Item 1</option>
  5. <option value=“2″>Item 2</option>
  6. <option value=“3″>Item 3</option>                       
  7. </select>
  8.  
  9. <!–Second Select Box–>
  10. <select class=“select”  name=“SaleOrderProduct[product_id]“ id=“product_id”>
  11. </select>
  12.  
  13.  
  14. //create script for show product base on product category
  15. <script>
  16.     jQuery(“#product_category_id”).change(function(){
  17.         var category = jQuery(“#product_category_id”).val();
  18.         var dataString = ‘category=’+ category;
  19.         jQuery.ajax({  
  20.           type: “POST”,  
  21.           url: “<?php echo Yii::app()->request->baseUrl; ?>/index.php/saleOrder/getProduct”,
  22.           data: dataString,
  23.           success: function(data)
  24.           {
  25.               jQuery(“#product_id”).html(data);
  26.           }
  27.                });
  28.         
  29.     });
  30. </script>

 

In "SaleOrderController.php -> getProduct" here getProduct is a function.
  1. //this function use for get product name based on product category
  2.     public function actionGetProduct()
  3.     {
  4.         $categoryId = $_POST[category];
  5.         //select data from your database base on $categoryId variable
  6.         $product = ProductInfo::model()->findBySql(“select id, product_name from product_info where status=’ACTIVE’ and deleted=’0′ and company_id=’”.Yii::app()->user->companyId.“‘ and product_category_id=’”.$categoryId.“‘”);
  7.         echo “<option value=’0′>— Select a product —</option>”;
  8.         for($i=0; sizeof($product)>$i; $i++)
  9.         {
  10.             echo “<option value=’”.$product->id.“‘>”.$product->product_name.“</option>”;
  11.         }
  12.     }


			

Set Extra Value in SESSION, Like(User Id not Name)

It is very easy to set extra value in yii framework $_SESSION. Just follow Line no-15 and 24, 25. The setState() function helps you.

  1. class UserIdentity extends CUserIdentity
  2. {
  3.     /**
  4.      * Authenticates a user.
  5.      * The example implementation makes sure if the username and password
  6.      * are both ’demo’.
  7.      * In practical applications, this should be changed to authenticate
  8.      * against some persistent user identity storage (e.g. database).
  9.      * @return boolean whether authentication succeeds.
  10.      */
  11.     private $_id;
  12.     
  13.     public function authenticate()
  14.     {        
  15.         $user=UserInfo::model()->findByAttributes(array(‘user_name’=>$this->username));
  16.         
  17.         if($user===null)
  18.             $this->errorCode=self::ERROR_USERNAME_INVALID;
  19.         else if($user->password!==$this->password)
  20.             $this->errorCode=self::ERROR_PASSWORD_INVALID;
  21.         else
  22.         {
  23.             $this->_id = $user->id;
  24.             $this->setState(‘userId’, $user->id);
  25.             $this->setState(‘typeId’, $user->type);
  26.             $this->errorCode=self::ERROR_NONE;
  27.         }
  28.         return !$this->errorCode;
  29.  
  30.     }
  31.     
  32.     public function getId()
  33.     {
  34.         return $this->_id;
  35.     }
  36. }

Rajib Kumar Rakhmit

Yii Framework

Rajib Kumar Rakhmit

Rajib Kumar Rakhmit

 

How to use PrettyPhoto Extension in Yii

It is so simple to use PrettyPhoto Extension in Yii Framework.

How to Install

Unzip the contents of the downloaded package below and copy its contents into your application’s protected/extensions folder.

How to Use

Once we have move the contents of the zipped file into the extensions folder we are ready to go. Use it at your own will into your views like this:

  1. <?PHP
  2. Yii::import(‘ext.jqPrettyPhoto’);
  3. jqPrettyPhoto::addPretty(‘.gallery a’,jqPrettyPhoto::PRETTY_GALLERY,jqPrettyPhoto::THEME_FACEBOOK);
  4. ?>

Wow! That was easy! Please allow me to explain the addPretty function. It comes with three parameters:

1) element/s selector: the JQUERY SELECTOR to the links you want to set pretty photo to. In the example above I had the following on the view’s HTML:

  1. <div class=“gallery”>
  2. <a href=“URL_TO_IMAGE_TO_OPEN”><img src=“URL_TO_IMAGE_TO_SHOW”/></a>
  3. </div>

2) The second parameter is of the value jqPrettyPhoto::PRETTY_GALLERY or jqPrettyPhoto::PRETTY_SINGLE, which tells the extension whether the selector will be a list of a gallery images or just a single file.

3) The third one is what theme to use -please refer to jqPrettyPhoto.php’s code to see more options on this one.
Download Now