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.

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.