Подскажите пожалуйста как связать де таблицы.
Есть таблицы Персонал, Страна, Города.
Staff {id, first_name, last_name, country, city, …};
Country { id, name};
City { id, id_country, name};
Один контроллер, три модели :
StaffContriller;
Staff;
Country;
City;
Нужно связать страну и город чтоб при создании сотрудника было два DropDownList { Country, City}, при выборе страны загружались города этой страны.
Я в yii и php новичок, не могу найти подробной инструкции, ищу целый день, уже перепробовал все что есть в сети, ничего не получается.
Помогите пожалуйста, буду очень благодарен!
Что я не так делаю?
Контроллер
public function actionDynamiccities()
{
$data = City::model()->findAll('id_country= :id_country', array(':id_country' => (int) $_POST['StaffController']['id_country']));
$data = CHtml::listData($data, 'id', 'name');
foreach($data as $id => $name) {
echo CHtml::tag('option', array('value' => $id), CHtml::encode($name), true);
}
}
Модель Country
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'city' => array(self::HAS_MANY, 'City', 'id_country'),
);
}
Модель City
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'country' => array(self::BELONGS_TO, 'Country', 'id'),
);
}
Представление
$list = CHtml::listData(Country::model()->findAll(), 'id', 'name');
echo CHtml::dropDownList('id_country', '', $list, array('empty'=>'-- Select Country --'),
array(
'ajax' => array(
'type' => 'POST',
//'url' => $this->createUrl('StaffController/dynamiccities'),
'url' => CController::createUrl('StaffController/dynamiccities'),
'update'=>'#modification_id',
) ) );
echo '<br>';
echo CHtml::dropDownList('modification_id', '', array());