Hi:
I’m new to PHP and Yii so please bear with me, and any help is appreciated.
Tables
countries:
-
country_code char(2) (pk)
-
country_name varchar(64)
regions:
-
region_id smallint (pk)
-
region_country_code char(2) (fk->countries.country_code)
-
region_code varchar(3)
-
region_name varchar(64)
min_wage_region:
-
min_wage_id int (pk)
-
min_wage_region_id smallint (fk->regions.region_id)
-
min_wage_rate_hr decimal(8,4)
In the min_wage_region create view (_form.php) I have the following code:
<div class="row">
<?php echo CHtml::activeLabelEx($model,'min_wage_region_id'); ?>
<?php
$modelCountries = Countries::model()->findAll(array('order' => 'country_code'));
$listCountries = CHtml::listData($modelCountries, 'country_code', 'country_name');
echo CHtml::dropDownList('selected_country', '', $listCountries, array(
'prompt' => '(Select country)',
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('regions/findregionsbycountry'),
'update'=>'#regions_list',
'data'=>'js:jQuery(this).serialize()', // only send country, not whole form
)
)
);
echo '<br />';
echo CHtml::textArea('regions_list', ''); // will be replaced by a dropDownList when it works!
?>
<?php echo CHtml::error($model,'min_wage_region_id'); ?>
</div>
In RegionsController.php I have the following:
... in function accessRules()
array('allow', // allow authenticated user to perform these actions
'actions'=>array('create','update','findregionsbycountry'),
'users'=>array('@'),
),
... and added this function:
/**
* Returns a list of regions given the country code.
*/
public function actionFindRegionsByCountry()
{
if(isset($_POST['selected_country']))
{
return Regions::model()->findCountryRegions($_POST['selected_country']);
}
}
Finally, I added this function to the model Regions.php:
/**
* Returns a list of regions given the country code.
*/
public function findCountryRegions($reg_ctry_code)
{
// search uses parameter instead of foreign key relationship
$criteria = new CDbCriteria(array(
'select'=>'region_id, region_name',
'condition'=>'region_country_code=:_param',
'order'=>'region_code ASC',
'params'=>array(':_param'=>$reg_ctry_code),
));
return CHtml::listData($this->findAll($criteria), 'region_id', 'region_name');
}
The problem is that the textArea in the view comes up empty. I have tested the model function in yiic’s command line and it works. I have also tested the view/controller/model message passing (using a constant string and also $_POST) and they work. I don’t know what’s wrong here, please help.
Thanks,
Jose