Sunday, March 9, 2014

Yii-call-function-from-other-class

In this tutorial i will show how to call a method from a class

first we should import the class.

after that we should instantiate the imported class.

then we can call method via instance.

Example,


class AnotherController extends Controller
{

public function __construct($id=NULL)
{

}

public function message()
{
return 'Message from Another Controller';
}
}

?>

Above lines shows a class(Controller). assume that this class is placed in side

the contollers folder.

we want to access method "message" from another controller.

solutioin is ,

public function messageFromAnotherClass()
      {
     
  Yii::import("application.controllers.AnotherController");
  $another = new AnotherController();  
  return $message = $another->message();
      }

yii-create-cutom-application-component


In this tutoial , i will show you how to create a simple custom Application Component.

First of all create a file named simple with following codes and put the file in the path

protected/components/Simple.php


class Simple  extends CApplicationComponent
{

    public function init()
    {
echo 'Simple Initiated
';
    }

    public function message()
    {
      return 'Message from Simple';
    }
   
   
}


?>

do the following changes int the main.php under config folder

'components'=>array(
........
            'simple'=>array('class'=>'Simple'),
.........

),


Now its time to call method in the created component.

steps are,

echo Yii::app()->simple->message();


output is

Simple Initiated
Message from Simple

Thursday, March 6, 2014

Yii-cgridview-table-css-class

By default Yii Cgridview have a spcific CSS formatting in gridview's table.

But we can apply css table class whatever we want to display.

we can apply it by htmlOptions array.

Example,

<?php $this->widget('zii.widgets.grid.CGridView', array(
'id'=>'account-grid',
'dataProvider'=>$model->search(),
 'htmlOptions' => array('class' => 'table table-hover table-striped table-bordered table-condensed'),
'filter'=>$model,
'columns'=>array(
'id',
'account_name',
array(
'class'=>'CButtonColumn',
),
),
)); ?>

yii-model-limited-rows

Yii model fetch limited rows Example

Assume that we have a model with two columns, they are id and account_name

$datas = Account::model()->findAll();

echo CJSON::encode($datas);

[{"id":"1","account_name":"Tea"},{"id":"2","account_name":"Food"},{"id":"3","account_name":"Travel"},{"id":"4","account_name":"Pettycash"}]

The above line shows the values in  the model.

we want to display only limited rows.

Lines are a follows,

$criteria = new CDbCriteria;
$criteria->limit = "3";
$datas = Account::model()->findAll($criteria);

echo CJSON::encode($datas);

ouput is

[{"id":"1","account_name":"New Tea"},{"id":"2","account_name":"Food"},{"id":"3","account_name":"Travel"}]

Yii-fetch-rows-order-by

Yii model order by column example.

Assume that we have a model with two columns, they are id and account_name

$datas = Account::model()->findAll();

echo CJSON::encode($datas);

[{"id":"1","account_name":"Tea"},{"id":"2","account_name":"Food"},{"id":"3","account_name":"Travel"},{"id":"4","account_name":"Pettycash"}]

The above line shows the values in the model.

we want show values that are ascending order by account_name column;

Lines are as follows,

$criteria = new CDbCriteria;
$criteria->order = "account_name asc";
$datas = Account::model()->findAll($criteria);

echo CJSON::encode($datas);

// output is

[{"id":"2","account_name":"Food"},{"id":"1","account_name":"New Tea"},{"id":"4","account_name":"Pettycash"},{"id":"3","account_name":"Travel"}]

Yii-update-model-by-primary-key

Yii model Update Row By primary Key Example

Assume that we have a model with two columns, they are id and account_name

$datas = Account::model()->findAll();

echo CJSON::encode($datas);

[{"id":"1","account_name":"Tea"},{"id":"2","account_name":"Food"},{"id":"3","account_name":"Travel"},{"id":"4","account_name":"Pettycash"}]

The above line shows the values in the model.

we want to update  account_name by Primary key.

line are as follows,

$pk=1;

$single = Account::model()->findByPk($pk);

$single->account_name = "New Tea";  // actual value is Tea

$single->save();

after These lines we got following value. while checking.....

$single = Account::model()->findByPk($pk);

echo $single->account_name;

// output is New Tea

Yii-count-model-rows-by-attribute

Yii Count Records by attribute Example

Assume that we have a model with two columns, they are id and account_name

$datas = Account::model()->findAll();

echo CJSON::encode($datas);

[{"id":"1","account_name":"New Tea"},{"id":"3","account_name":"Travel"},{"id":"4","account_name":"Pettycash"},{"id":"13","account_name":"Travel"}]

The above line shows the values in the model.

we want to count rows by  specific acount_name.

line are as follows,


$data = Account::model()->findAllByAttributes(array(
                  'account_name'=> 'Travel',
               ));

echo count($data);

//output is 2

yii-model-fetch-row-by-primary-key

Yii model fetch Row By primary Key Example

Assume that we have a model with two columns, they are id and account_name

we need to fecth a particular row based on primary key value.

$datas = Account::model()->findAll();

echo CJSON::encode($datas);

[{"id":"1","account_name":"New Tea"},{"id":"2","account_name":"Food"},{"id":"3","account_name":"Travel"},{"id":"4","account_name":"Pettycash"}]

The above line shows the values in the model.

we want to get acount_name that have a primary key value is 1.

lines are,


$pk=1;
$single = Account::model()->findByPk($pk);
echo $single->account_name;


// output is New Tea