Yii


How to pre select radioButtonListRow and checkboxListRow with Yii 2

Looking at the code in Yii for radioButtonListRow and checkboxListRow it’s not apparent how to preselect these form elements with previously selected data.

<?php echo $form->checkboxListRow($model, 'id', CHtml::listData(models\web_data\Program::model()->findAll(array('order' => 'name')), 'id', 'name')); ?>

The solution is to set the model’s property to your selected values in the controller. You can use a scalar value or an array of values. For the above line of code, we can use the below –

$model->id = array(2,3);

or

$model->id = 2;

Adding a confirm delete popup to Yii admin form

I wanted to share this bit I was able to get working today with my Yii application. Add the JavaScript confirm dialog to the “htmlOptions” array key as below –

<?php $this->widget('bootstrap.widgets.TbGridView',array(
	'id'=>'menu-block-grid',
	'dataProvider'=>$model->search(),
	'type'=>'striped bordered condensed',
	'filter'=>$model,
	'columns'=>array(
		'id',
		'location',
		'sort_order',
		'name',
		'href',
		array(
			'class'=>'bootstrap.widgets.TbButtonColumn',
			'htmlOptions'=>array('onclick'=>'return confirm("Are you sure?")'),
		),
	),
)); ?>

Yii multisite – point domain to modules 2

I’m rewriting a couple existing and related websites into the Yii framework. They’re for the same company and share the same databases, so I’ve setup each of the sites as a module. I needed each site’s existing domain name to point at a module – subdomains or subdirectories in the url would not be acceptable. Also, each of these sites has an administration site behind it. I could have set these up in Yii as a sub module, but elected to instead just create another module for them.

Take a look at the below code from the /protected/config/local.php file I created for my local environment

return CMap::mergeArray(
require (dirname(__FILE__) . '/main.php'), 
array(
    'components' => array(
		'urlManager'=>array(
			'rules' => array(
 
			    //front end of sites
				'http://<module:\w+>.localhost/' => '<module>/default',
				'http://<module:\w+>.localhost/<controller:\w+>' => '<module>/<controller>/',
				'http://<module:\w+>.localhost/<controller:\w+>/<action:\w+>' => '<module>/<controller>/<action>',
				'http://<module:\w+>.localhost/<controller:\w+>/<action:\w+>/<id:\d+>' => '<module>/<controller>/<action>',
 
				//admin subdomains for admin modules
				'http://admin.<module:\w+>.localhost/' => '<module>admin/default',
				'http://admin.<module:\w+>.localhost/<controller:\w+>' => '<module>admin/<controller>/',
				'http://admin.<module:\w+>.localhost/<controller:\w+>/<action:\w+>' => '<module>admin/<controller>/<action>',
				'http://admin.<module:\w+>.localhost/<controller:\w+>/<action:\w+>/<id:\d+>' => '<module>admin/<controller>/<action>',
			),
		),
    )
));

The trick to get the above working is to name your modules the same as your domain, so if your domain is abc.com, then your module name would be abc. To access your administration site, you’d create an abcadmin module and put admin.abc.localhost in the url. You can change the TLD extension for your needs.


Accessing models in Yii migrations

Yii migrations are meant for database changes, but they are so handy wouldn’t it be nice if they handled data changes and other conversion scripts as well? You can access Yii’s models by adding Yii::import(‘application.models.*’); to the up() function like below.

public function up() {
    Yii::import('application.models.*');
    $userModel = new User();
    $users = $userModel->findAll();
    .....
}