Help - Forms Using Related Tables

Hi all,

As I am new to both Yii and MVC I am having difficulty in getting forms working that have related tables.

Basically, what I am trying to do is have 2 forms on one page that both have their own table (and model) but are related by a foreign key.

My 2 tables are TestParent and TestChild. The child table is related to the parent table by a foreign key on the child_parent_id field and the relation is one-to-one.

When a user clicks on the view file of the parent the page will display data from both models but will show the correct child record that matches the id of the parent record.

My parent controller actions are as follows:


      public function actionCreate() {


            $a = new TestParent;

            $b = new TestChild;


            if (isset($_POST['TestParent'], $_POST['TestChild'])) {


                  $a->setAttributes($_POST['TestParent']);

                  $b->setAttributes($_POST['TestChild']);


                  $valid = $a->validate();

                  $valid = $b->validate() && $valid;


                  if ($valid) {


                        if ($a->save() && $b->save())  {


                              if (isset($_POST['save'])) {


                                    $a->save(false);

                                    $b->save(false);


                                    $this->redirect(array('update', 'id' => $a->parent_id));

                              }


                              if (isset($_POST['saveclose'])) {


                                    $a->save(false);

                                    $b->save(false);


                                    $this->redirect(array('index'));

                              }

                        }

                  }

            }


            $this->render('create', array(

                'a' => $a,

                'b' => $b,

            ));

      }


      public function actionUpdate($id) {




            $a = $this->loadModel($id, 'TestParent');

            $b = new TestChild;




            if (isset($_POST['TestParent'], $_POST['TestChild'])) {


                  $a->setAttributes($_POST['TestParent']);

                  $b->setAttributes($_POST['TestChild']);


                  if ($a->save()) {


                        if (isset($_POST['save'])) {

                              $this->redirect(array('update', 'id' => $a->parent_id));

                        }


                        if (isset($_POST['saveclose'])) {

                              $this->redirect(array('index'));

                        }

                  }

            }


            $this->render('update', array(

                'a' => $a,

                'b' => $b,

            ));

      }

My update view file is as follows:


<?php

$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(

    'id' => 'parent-form',

    'enableAjaxValidation' => false,

    'htmlOptions' => array(

    'class' => 'form-horizontal',

    ),

    ));

?>


<?php $this->renderPartial('//testParent/buttons/update_buttons', array('a' => $a)); ?>


<?php $this->setPageTitle('Update Parent'); ?>


<div class="alpha span5">


      <div class="control-group">

            <?php echo $form->labelEx($a, 'parent_field_1', array('class' => 'control-label')); ?>

            <div class="controls">

                  <?php echo $form->textField($a,  'parent_field_1'); ?>

                  <?php echo $form->error($a, 'parent_field_1' ); ?>

            </div>

      </div>

      <div class="control-group">

            <?php echo $form->labelEx($a, 'parent_field_2', array('class'  => 'control-label')); ?>

                    <div class="controls">

                          <?php echo $form->textField($a, 'parent_field_2'); ?>

                          <?php echo $form->error($a, 'parent_field_2'); ?>

                    </div>

              </div>

              <div class="control-group">

                    <?php echo $form->labelEx($a, 'parent_field_3', array  ( 'class' =>  'control-label')); ?>

                    <div class="controls">

                          <?php echo $form->textField($a, 'parent_field_3'); ?>

                          <?php echo $form->error($a, 'parent_field_3'); ?>

                    </div>

              </div>

              <div class="control-group">

                    <?php echo $form->labelEx($a, 'parent_field_4', array  ('class'  =>  'control-label')); ?> 

                    <div class="controls">

                          <?php echo $form->textField($a, 'parent_field_4'); ?>

                          <?php echo $form->error($a, 'parent_field_4'); ?>

                    </div>

              </div>

        </div>


        <div class="alpha span5" style="border:1px #ddd solid;padding:12px;">

              <div class="control-group">

                    <?php echo $form->labelEx($b, 'child_parent_id', array  ('class' =>  'control-label')); ?>

                    <div class="controls">

                          <?php echo $form->textField($b, 'child_parent_id'); ?>

                          <?php echo $form->error($b, 'child_parent_id'); ?>

                    </div>

              </div>

              <div class="control-group">

                    <?php echo $form->labelEx($b, 'child_checkbox_1', array( 'class' => 'control-label')); ?>

                    <div class="controls">

                          <?php echo $form->checkBox($b, 'child_checkbox_1'); ?>

                          <?php echo $form->error($b, 'child_checkbox_1'); ?>

                    </div>

              </div>

              <div class="control-group">

                    <?php

                    echo $form->labelEx($b, 'child_checkbox_2', array('class' => 'control-label')); ?>

            <div class="controls">

                  <?php echo $form->checkBox($b, 'child_checkbox_2'); ?>

                  <?php echo $form->error($b, 'child_checkbox_2'); ?>

            </div>

      </div>

      <div class="control-group">

            <?php echo $form->labelEx($b, 'child_checkbox_3', array('class' => 'control-label')); ?>

            <div class="controls">

                  <?php echo $form->checkBox($b, 'child_checkbox_3'); ?>

                  <?php echo $form->error($b, 'child_checkbox_3'); ?>

            </div>

      </div>

      <div class="control-group">

            <?php echo $form->labelEx($b, 'child_checkbox_4', array('class' => 'control-label')); ?>

            <div class="controls">

                  <?php echo $form->checkBox($b, 'child_checkbox_4'); ?>

                  <?php echo $form->error($b, 'child_checkbox_4'); ?>

            </div>

      </div>

      <div class="control-group">

            <?php echo $form->labelEx($b, 'child_checkbox_5', array('class' => 'control-label')); ?>

            <div class="controls">

                  <?php echo $form->checkBox($b, 'child_checkbox_5'); ?>

                  <?php echo $form->error($b, 'child_checkbox_5'); ?>

            </div>

      </div>

      <div class="control-group">

            <?php echo $form->labelEx($b, 'child_checkbox_6', array('class' => 'control-label')); ?>

            <div class="controls">

                  <?php echo $form->checkBox($b, 'child_checkbox_6'); ?>

                  <?php echo $form->error($b, 'child_checkbox_6'); ?>

            </div>

      </div>

      <div class="control-group">

            <?php echo $form->labelEx($b, 'child_checkbox_7', array('class' => 'control-label')); ?>

            <div class="controls">

                  <?php echo $form->checkBox($b, 'child_checkbox_7'); ?>

                  <?php echo $form->error($b, 'child_checkbox_7'); ?>

            </div>

      </div>

      <div class="control-group">

            <?php echo $form->labelEx($b, 'child_checkbox_8', array('class' => 'control-label')); ?>

            <div class="controls">

                  <?php echo $form->checkBox($b, 'child_checkbox_8'); ?>

                  <?php echo $form->error($b, 'child_checkbox_8'); ?>

            </div>

      </div>

      <div class="control-group">

            <?php echo $form->labelEx($b, 'child_checkbox_9', array('class' => 'control-label')); ?>

            <div class="controls">

                  <?php echo $form->checkBox($b, 'child_checkbox_9'); ?>

                  <?php echo $form->error($b, 'child_checkbox_9'); ?>

            </div>

      </div>

      <div class="control-group">

            <?php echo $form->labelEx($b, 'child_checkbox_10', array('class' => 'control-label')); ?>

            <div class="controls">

                  <?php echo $form->checkBox($b, 'child_checkbox_10'); ?>

                  <?php echo $form->error($b, 'child_checkbox_10'); ?>

            </div>

      </div>

</div>


<?php

$this->endWidget();

?>

What I am trying to do is when a user clicks on - testParent/update/8 the record from the child model with 8 in the child_parent_id will display.

Does anyone know a good Yii way to link these models so that the view file shows the correct record?

Many thanks in advance.