Hello Everyone!
I am fairly new to the yii framework and I’ve been wanting to do certain things in my project the “yii” way.
I’ve encountered a problem where I need a dependent dropdown list for a secondly related table. There is a diagram of the related tables in attachment. Please see it to fully understand the problem.
4194
So I have the following tables:
[b]Sector
Company
CompanySector
Employee[/b]
And the following relations:
Sector belongs to CompanySector
Company belongs to CompanySector
CompanySector belongs to Employee
Now, for each company there is a list of sectors available for that company (this data is available in CompanySector).
When a new employee is being inserted, I need to specify in which company and sector the employee belongs to. To do this, I want a dropdown list that will display all the companies. Upon selecting a company, the list of all available sectors for that company should be displayed in another dropdown list.
Then, when both company and sector are selected, I will make a query that will find the primary key of the CompanySector record where ID_Company and ID_Sector are the values from the dropdown lists (and then insert it in F_ID_CompanySector in Employee). I need to make the dependent dropdownlists first however.
I tried this article but i had no luck in solving my problem.
I added the following variables in my Employee model:
public $ID_Company;
public $ID_Sector;
I have the following function in the Company model which lists all values for the company (used in dropdown list):
public function getCompanyOptions()
{
return CHtml::listData(Company::model()->findAll(),
'ID_Company', 'Company_Name');
}
And i have the following in the _from of Employee for the company and sector dropdowns
<td><?php echo $form->labelEx($model,'ID_Company'); ?></td>
<td>
<?php echo $form->dropDownList($model,'ID_Company',
Company::model()->getCompanyOptions(), array('prompt' => 'Select Company')); ?>
<?php echo $form->error($model,'ID_Company'); ?>
</td>
<td><?php echo $form->labelEx($model,'ID_Sector'); ?></td>
<td>
<?php echo $form->dropDownList($model,'ID_Sector',
Sector::model()->getSectorOptions(), array('prompt' => 'Select Sector')); ?>
<?php echo $form->error($model,'ID_Sector'); ?>
</td>
If you need any more information please let me know.
Thank you for your time.