Yii PHP Framework – snippets

Some random Yii stuff, will continue to update

URL link info:

You can use this method:

echo Yii::app()->getBaseUrl();

To get the location of the web root (more specifically, it’s where the index.php file lives that started the Yii app) and use that for to prefix your assets. The other option is to simply use “/”, but the above will offer a bit more portability.

Get URL request:

$url = Yii::app()->getRequest()->requestUri;
echo ucwords(trim($url, '/'));

To get the Yii route request, e.g., controller/action:

$url = Yii::app()->getUrlManager()->parseUrl(Yii::app()->getRequest());

just the action:



Yii::app()->controller->redirect(array('controller/action', 'param1'=>$value));

so from a controller class, you can use “$this” and the url/array method.

Zii widgets

Since the auto-generated admin pages changed to include the sometimes helpful search widget, the way I used to set pagination wasn’t working (setting $dataProvider->pagination->pageSize value in the controller) — the solution, however is pretty simple:


$this->widget('zii.widgets.grid.CGridView', array(


	$dp = $model->search();
	$this->widget('zii.widgets.grid.CGridView', array(

custom query for data provider:

$dataProvider = new CSqlDataProvider($sql, array(

grid update function:
see: jquery.yiigridview.js

grid custom columns:


some database functions:

  • find by primary key:
    $foobar = Project::model()->findByPk($this->numnum);
  • Use model() construct to return array data:

    This is a pretty neat construct to use model() with existing scopes and pretty easily add custom conditions (make sure you add them, i.e., “.=“) with params via queryAll and get an array instead of an object as a result.

    $model = Holidays::model()->comp();
    $model->dbCriteria->condition .= " AND date BETWEEN :start_day AND :end_day";
    $model->dbCriteria->order = 'date ASC';
    $model->dbCriteria->params = array(':start_day'=>'2011-07-01', ':end_day'=>$end_cal_date->format('Y-m-d'));
    #echo '<pre>$model->dbCriteria: '; print_r($model->dbCriteria); echo '</pre>'; exit;
    $holidays = $model->getCommandBuilder()
    	->createFindCommand($model->tableSchema, $model->dbCriteria)
    $h = $holidays; echo '<pre>$h: '; print_r($h); echo '</pre>';
    queryAll(boolean $fetchAssociative=true, array $params=array ( ))

    show db queries

    Tip: While AR allows us to perform database operations without writing cumbersom SQL statements, we often want to know what SQL statements are executed by AR underneath. This can be achieved by turning on the logging feature of Yii. For example, we can turn on CWebLogRoute in the application configuration, and we will see the executed SQL statements being displayed at the end of each Web page. We can set CDbConnection::enableParamLogging to be true in the application configuration so that the parameter values bound to the SQL statements are also logged.

  • useful function for rendering a partial view within a function or class:



  • Leave a Reply