Yii Built In functions – Generic

// For dropdown list
$list=CHtml::listData(SysState::model()->findAll(), 'state_id', 'state_name');
echo $form->dropDownList($model,'state_id', $list, array('empty'=>'Select state'));

// dynamic dropdown populate
Below lines create the country dropdown which updates state dropdown
'POST', //request type
'url' => Yii::app()->createUrl('manager/events/dynamicStates/'), //url to call.
//Style: ii::app()->createUrl('currentController/methodToCall')
'update' => '#Events_state_id', //selector to update
'data'=> array('country_code'=>'js:this.value')
//leave out the data key to pass all form values through
)
?>
dropDownList($model,'country_code', $list['countries'], array('empty'=>'--none--', 'class'=>'form-control', 'ajax'=>$ajaxStatePopulate)) ?>
error( $model, 'country_code'); ?>

The above code updates the below line
dropDownList($model,'state_id', $list['states'], array('empty'=>'--none--', 'class'=>'form-control')) ?>
error( $model, 'state_id'); ?>

Called url will have the below code
$result = States::model->findAll();
$data = CHtml::listData($result,'id','name');
//return CHtml::listData($result, 'code', 'name');print_r($data);
echo CHtml::tag('option',
array('value'=>''),CHtml::encode('--none--'),true);
foreach($data as $value=>$name)
{
echo CHtml::tag('option',
array('value'=>$value),CHtml::encode($name),true);
}

// NEXT
// To update the of the value in text field
pre_reg_start_date = Common::formatDbDate($model->pre_reg_start_date)?>
textField( $model, 'pre_reg_start_date', array('class'=>'form-control selectDate')); ?>

// NEXT : update label
labelEx( $model, 'pre_reg_start_time', array('label'=>'Time') ); ?>

// NEXT : create link with id
$event_id))?>" class="btn btn-primary">Link

// NEXT : Set your messages in a controller:
Yii::app()->user->setFlash('success', "Data1 saved!");
Yii::app()->user->setFlash('error', "Data2 failed!");
Yii::app()->user->setFlash('notice', "Data3 ignored.");

Display them in your view:
user->getFlashes() as $key => $message) {
echo '

' . $message . "

\n";
}
?>

// NEXT : Create url examples
$url=$this->createUrl($route,$params); // syntax

$url = Yii::app()->createUrl('site/index');
index.php?r=site/index

$url = Yii::app()->createUrl('site/index', array('id'=>100));
index.php?r=site/index&id=100

Create URL from Controller
Yii::app()->controller->createUrl("index", array("id"=>100));
index.php?r=site/index&id=100

create url withing same controller with id as parameter
$this->createUrl('index',array('id'=>100));

Create absolute URL:
In order to create an absolute path url you need to use createAbsoluteUrl() function:
Yii::app()->createAbsoluteUrl('site/index',array('id'=>100));
http://yourdomain.com/index.php?r=site/index&id=100

echo CHtml::link('text', array('site/index', 'id'=>100));
text

Redirection example
$this->redirect(array('site/index','id'=>100));
index.php?r=site/index&id=100

$this->redirect(array('site/index','id'=>100));

// Get base url
Yii::app()->baseUrl

// get current url before ? mark
Yii::app()->request->pathInfo

// Theme url
Yii::app()->theme->baseUrl

// get the theme name
Yii::app()->theme->name

// get base path
$this->getBasePath()

// home page url
$this->getHomeUrl()

// url related properties
Yii::app()->request->pathInfo
Yii::app()->request->url
Yii::app()->request->requestUri
Yii::app()->request->queryString
Yii::app()->request->isSecureConnection
Yii::app()->request->isPostRequest
Yii::app()->request->isAjaxRequest
Yii::app()->request->serverName
Yii::app()->request->urlReferrer
Yii::app()->request->userHostAddress // IP address

// NEXT : rules example to validate the date and time
array('wave_date', 'type', 'type'=>'date', 'dateFormat'=>'MM/dd/yyyy'),
array('wave_time', 'type', 'type'=>'time', 'timeFormat'=>'hh:mm a'),

Text field

Example:
'form-control')); ?>

Text field using Form. It does same as above
textField( $model, 'name', array('class'=>'form-control')); ?>

Text field without form and model

Creating a simple widget
Step 1: components/BreadCrumb.php:
render('breadCrumb');
}

}
?>
Step 2: components/views/breadCrumb.php

Step 3: Call from your actuall view as below
widget('application.components.BreadCrumb', array(
'crumbs' => array(
array('name' => 'Home', 'url' => array('site/index')),
array('name' => 'Login'),
),
'delimiter' => ' → ', // if you want to change it
)); ?>

// NEXT : Logging
Yii::log($message, $level, $category);
Yii::trace($message, $category);

// NEXT : end application
Yii::app()->end(); // does

// Set get session value
Yii::app()->session["lang"] = "English";
echo Yii::app()->session["lang"]

Leave a Reply