Passing PK from one form to another

My first post!

Searched this forum front to back to find this. Not sure how to do this.

2 db tables:

project: id, name, desc

risk: id, name, desc, project_id as foreign key

relationship = project may have 1 or more risks.

I want to be able to enter a project. Once it is saved, then go to the risk form (seperate form) and enter risks for that project. But in my mind, i should pass the id from the project form to the risk form and prepopulate the project_id foreign key without using a project dropdown.

How can i do this? Is there a standard way to handle such a parent child relationship situation using yii? Any help is appreciated.

Pass project’s ID via GET string and either store it to hidden field or leave in GET (and submit to this url)

Something like:

public function actionCreateProject() {


    if ($project->save()) {

        $this->redirect(array('createRisk', 'projectId' => $project->id));



public function actionCreateRisk($projectId) {


    $risk = new Risk;

    $risk->projectId = $projectId;


    if ($risk->save()) {




Thanks for this quick response. I get it. I can see how this works.

I want to pass this variable to many forms.

The user inputs a project. Then can move on the create risks, team, costs, etc. all on different forms and all related to the project (project_id FK).

Do you go on and pass the value from one form to the next, or do you set a session variable to make this easier. And then if the user selects/creates a new project, then reset that session variable?

Session is not a reliable storage, so you should be very careful.

I suggest you to pass the project id through the related forms ina a GET (or POST) parameter. First, define your UI and navigation, then think in a practical way to pass the parameter using the navigation links. For instance, you can use TABS to navigate between related forms of a main recod (the project). In this case you just create the links adding the project ID parameter.