Giix Controller with JToggleColumn

Hello If you are using JToggleColumn extension with GxController then the loadModel syntax look like following:

$this->loadModel($id, ‘ModelName’),

So if we want to use JToggleColumn extension following changes needed in extension so we can dynamically use toggle button.

changes in JToggleColumn.php

1. declare public variable called

public $model_name = null;

2. put following code in init() function for exception

 if ($this->model_name === null)
            throw new CException(Yii::t(‘toggle_column’, ‘Model attribute (“model_name”) must be specified for CToggleColumn.’));
 3. put following code in initDefaultButtons() function

$this->toggle_button = array(
                ‘url’ => ‘Yii::app()->controller->createUrl(“‘ . $this->action . ‘”,array(“id”=>$data->primaryKey,”attribute”=>”‘ . $this->name . ‘”,”model_name”=>”‘.$this->model_name.'”))’,
                ‘options’ => array(‘class’ => $this->name . ‘_toggle’),
            );

changes in ToggleAction.php

public function run($id,$attribute,$model_name) {
        
        if(Yii::app()->request->isPostRequest)
        {
            // we only allow deletion via POST request
            $model = $this->controller->loadModel($id,$model_name);
            $model->$attribute = ($model->$attribute==0)?1:0;
            $model->save(false);
            
            // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
            if(!isset($_GET[‘ajax’]))
                $this->redirect(isset($_POST[‘returnUrl’]) ? $_POST[‘returnUrl’] : array(‘admin’));
        }
        else
        throw new CHttpException(400,’Invalid request. Please do not repeat this request again.’);
    }

This is Done..

If you want to put togglebutton in gridview this the column code look like following…

array(
                    ‘class’=>’JToggleColumn’,
                    ‘name’=>’active’, // boolean model attribute (tinyint(1) with values 0 or 1)
                    ‘model_name’=>’ModelName’,             //here is my model name so click on button change done in table related this model
                    ‘filter’ => array(‘1’ => ‘Yes’,’0′ => ‘No’), // filter
            ‘action’=>’toggle’, // other action, default is ‘toggle’ action
            ‘checkedButtonLabel’=>Yii::app()->baseUrl.’/images/checked.png’, // tooltip
                    ‘uncheckedButtonLabel’=>Yii::app()->baseUrl.’/images/unchecked.png’,
                ‘labeltype’=>’image’,
                    ‘htmlOptions’=>array(‘style’=>’text-align:center;min-width:60px;’)
                ),

 

So by this changes you can dynamically toggle the column value..

Thankx…

Concate String in CHtml::listData

I have table with following fields :
id
roll_no
name
branch

Here is my data :

1    7005    Ravi          CE
2    7006    Parth        CE
3    7007    Yashpal    IT
4    7008    Sukunj      IT

I want to display data in dropdown with following format
Ravi-7005(CE)
Parth-7006(CE)
Yashpal-7007(IT)
Sukunj-7008(IT)     using CHtml::listData

Here is my view file code
<?php
    echo CHtml::dropDownList(‘stud_id’, ”,CHtml::listData(Student::model()->findAll(), ‘id’, ‘concate’)
); ?>

Here is my model file’s concate method

    public function getConcate()
    {
        return $this->name.”-“.$this->roll_no.”(“.$this->branch.”)”;
// or whatever format you want to display
    }

By create this type of method you can concate any type of string that you need.
Here is the output.

Image

List Of Query Commands for getting Data from database

Get Single Record.

1. ModelName::model()->findByPk($primaryKey);
2. ModelName::model()->findByAttributes(array(‘columnName1’=>$value1, ‘columnName2’=>$value2));
3. ModelName::model()->find(array(‘condition’=>’columnName1=’.$value1. ‘ and columnName2=’.$value2));
4. Yii::app()->user->db->createCommand()
->select(‘*’)
->from(‘tableName’)
->where(‘columnName1=’.$value1.’ and columnName2=’.$value2)
->queryRow();

Get Last Single Record.

1. ModelName::model()->findByAttributes(array(‘columnName1’=>$value1, ‘columnName2’=>$value2,array(‘order’=>’Auto_Increment_ID desc’)));
2. ModelName::model()->find(array(‘condition’=>’columnName1=’.$value1.’ and columnName2=’.$value2,  ‘order’=>’auto_Increment_ID desc’));
3. Yii::app()->user->db->createCommand()
->select(‘*’)
->from(‘tableName’)
->where(‘columnName1=’.$value1.’ and columnName2=’.$value2.’ order by auto_Increment_ID’)
->queryRow();

Get Multiple Records.

1. ModelName::model()->findAll(array(‘condition’=>’columnName1=’.$value1.’ and columnName2=’.$value2));
2. ModelName::model()->findAll(‘columnName1=’.$value1.’ and columnName2=’.$value2);
3. ModelName::model()->findAllByAttributes(array(‘columnName1’=>$value1,’columnName2’=>$value2));
4. ModelName::model()->findAllByAttributes(array(‘columnName1’=>$value1,’columnName2’=>$value2),
$condition= ‘columnName3 <> :aliasName3 AND columnName4 >= :aliasName4 OR columnName5 = :aliasName5’,
$params =array(
‘:aliasName3’ => $value3,
‘:aliasName4’ => $value4,
‘:aliasName5’ => $value5,
));
5. Yii::app()->user->db->createCommand()
->select(‘*’)
->from(‘tableName’)
->where(‘columnName1=’.$value1.’ and columnName2=’.$value2)
->queryAll();