Relation between tables

If i have two table

1-Employee{username,EmployeeID,statusID}

2-EmployeeStatus(StatusID,Name)

when i enter new employee i select name of status but i want to save its id in table?

How i can do it

thanks lot

If you use a drop down list to display the EmployeeStatus in your Employee Form like so


$form->dropDownList($model,'StatusID', CHtml::listData(EmployeeStatus::model()->findAll(),'StatusID','Name'));

It will add the StatusID to your Employee table when save your form

thanks alot

but if the employee may have more than one status .

so the tables will be as follow :

1-Employee(username,EmployeeID)

2-Status(StatusID,Name)

3-EmployeStatus(EmployeeId,StatusID)

How i can write relation in each model ?and how i can display status name and save its id and employee id in EmployeeStatus Table

thanks alot

Did you use Gii to create your models? If you did it should have defined the relations for you under function relations.

yes i use GII but when i generate models i find that the function relation is empty

and i try to write relation by my self .

what can i do?

have you defined the relations in the database?

if you are using MySQL, use InnoDB tables and stablish the relations, after that, use Gii to generate models

hope this helps

If for some reason you don’t want to explicitly declare your relationships in your database(I think you should though) you can manually write them. check out this, I recommend reading the entire guide it will help a lot.

thanks alot for every one but now i have anther question

if i construct this relation between this tables

1-Employee(username,EmployeeID)

2-Status(StatusID,Name)

3-EmployeStatus(EmployeeId,StatusID)

how i can display status name in employee form and then save selected status id and employee id in EmployeeStatus Table

assume that each employee have many status so we save status id and employee id in EmployeeStatus

I’m still new to this, but you could add a public property to your Employee model called $statusID add the

$statusID property to your list of safe attributes. Then use the drop down box to display the status names, after you save your employee model create a new EmployeeStatus object and then save that to the database with your employeeID and StatusId you got from your employee form, you could also use a nested form, there is an example from the Definitive Guide to Yii here. It might be

easier the way I explained though. You should probably use a transaction as well(assuming you are using

innoDB)