title: yii - insert data into two tables via single form
body: <p>I am very much a novice with PHP and the Yii framework. </p>
  
  <p>Click here to see a sample of my SQL database setup <a href="http://i.stack.imgur.com/W6qQj.png" rel="nofollow">http://i.stack.imgur.com/W6qQj.png</a> </p>
  
  <p>I have set up controllers and models for all three tables and have set up CRUD views from Gii for the Users table. I have read through this -> <a href="http://www.yiiframework.com/wiki/19/how-to-use-a-single-form-to-collect-data-for-two-or-more-models/" rel="nofollow">http://www.yiiframework.com/wiki/19/how-to-use-a-single-form-to-collect-data-for-two-or-more-models/</a> and tried multiple sets of code examples etc... and failed. The best I can do is get the create form to insert all the data into the users table but not the email table. I believe my short coming is in my UsersController.php -> public function actionCreate() section. Can someone help me out with a sample piece of code to make the information post into the email table? </p>
  
  <p>---EDIT---
  As requested below is the requested info...</p>
  
  <p>UsersController.php</p>
  
  <pre><code>public function actionCreate()
      {
          $model = new Users;
          if (isset($_POST['Users'])) {
              $model-&gt;attributes = $_POST['Users'];
                  $model-&gt;password=crypt($model-&gt;password,'salt');
                  $model-&gt;datecreated = new CDbExpression('NOW()');
              if ($model-&gt;save()) {
                  $modelEmail = new Email;
                  $modelEmail-&gt;attributes = $_POST['Email'];
                  $modelEmail-&gt;fer_users_id = $model-&gt;id;
                  if ($modelEmail-&gt;save())
                      $this-&gt;redirect(array('view', 'id' =&gt; $model-&gt;id));
              }
          }
          $this-&gt;render('create', array(
              'model' =&gt; $model,
          ));
      }
  </code></pre>
  
  <p>This is the users/_form.php view file:</p>
  
  <pre><code>&lt;div class="form"&gt;
  
  &lt;?php $form=$this-&gt;beginWidget('CActiveForm', array(
      'id'=&gt;'users-form',
      'enableAjaxValidation'=&gt;false,
  )); ?&gt;
  
      &lt;p class="note"&gt;Fields with &lt;span class="required"&gt;*&lt;/span&gt; are required.&lt;/p&gt;
  
      &lt;?php echo $form-&gt;errorSummary($model); ?&gt;
  
      &lt;div class="row"&gt;
          &lt;?php echo $form-&gt;labelEx($model,'username'); ?&gt;
          &lt;?php echo $form-&gt;textField($model,'username',array('size'=&gt;60,'maxlength'=&gt;255)); ?&gt;
          &lt;?php echo $form-&gt;error($model,'username'); ?&gt;
      &lt;/div&gt;
  
      &lt;div class="row"&gt;
          &lt;?php echo $form-&gt;labelEx($model,'password'); ?&gt;
          &lt;?php echo $form-&gt;passwordField($model,'password',array('size'=&gt;60,'maxlength'=&gt;255)); ?&gt;
          &lt;?php echo $form-&gt;error($model,'password'); ?&gt;
      &lt;/div&gt;
  
      &lt;div class="row"&gt;
          &lt;?php echo $form-&gt;labelEx($model,'firstname'); ?&gt;
          &lt;?php echo $form-&gt;textField($model,'firstname',array('size'=&gt;60,'maxlength'=&gt;255)); ?&gt;
          &lt;?php echo $form-&gt;error($model,'firstname'); ?&gt;
      &lt;/div&gt;
  
      &lt;div class="row"&gt;
          &lt;?php echo $form-&gt;labelEx($model,'lastname'); ?&gt;
          &lt;?php echo $form-&gt;textField($model,'lastname',array('size'=&gt;60,'maxlength'=&gt;255)); ?&gt;
          &lt;?php echo $form-&gt;error($model,'lastname'); ?&gt;
      &lt;/div&gt;
  
      &lt;div class="row"&gt;
          &lt;?php echo $form-&gt;labelEx(Email::model(),'emailaddress'); ?&gt;
      &lt;?php echo $form-&gt;textField(Email::model(),'emailaddress',array('size'=&gt;60,'maxlength'=&gt;255)); ?&gt;
      &lt;?php echo $form-&gt;error(Email::model(),'emailaddress'); ?&gt;
      &lt;/div&gt;
  
      &lt;div class="row"&gt;
                  &lt;?php echo $form-&gt;labelEx($model,'fer_roles_id'); ?&gt;
                  &lt;?php
                          echo $form-&gt;dropDownList($model, 'fer_roles_id', 
                          CHtml::listData(Roles::model()-&gt;findAll(), 'id', 'description'),
                          array('class' =&gt; 'my-drop-down', 'options' =&gt; array('2' =&gt; array('selected' =&gt; "selected")
  
                                      )
                                  )
                              );
                  ?&gt; 
      &lt;/div&gt;
  
      &lt;div class="row buttons"&gt;
          &lt;?php echo CHtml::submitButton($model-&gt;isNewRecord ? 'Create' : 'Save'); ?&gt;
      &lt;/div&gt;
  
  &lt;?php $this-&gt;endWidget(); ?&gt;
  
  &lt;/div&gt;
  </code></pre>