yii - 通过单一表单将数据插入两个表

I am very much a novice with PHP and the Yii framework.

Click here to see a sample of my SQL database setup http://i.stack.imgur.com/W6qQj.png

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 -> http://www.yiiframework.com/wiki/19/how-to-use-a-single-form-to-collect-data-for-two-or-more-models/ 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?

---EDIT--- As requested below is the requested info...

UsersController.php

public function actionCreate()
    {
        $model = new Users;
        if (isset($_POST['Users'])) {
            $model->attributes = $_POST['Users'];
                $model->password=crypt($model->password,'salt');
                $model->datecreated = new CDbExpression('NOW()');
            if ($model->save()) {
                $modelEmail = new Email;
                $modelEmail->attributes = $_POST['Email'];
                $modelEmail->fer_users_id = $model->id;
                if ($modelEmail->save())
                    $this->redirect(array('view', 'id' => $model->id));
            }
        }
        $this->render('create', array(
            'model' => $model,
        ));
    }

This is the users/_form.php view file:

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'users-form',
    'enableAjaxValidation'=>false,
)); ?>

    <p class="note">Fields with <span class="required">*</span> are required.</p>

    <?php echo $form->errorSummary($model); ?>

    <div class="row">
        <?php echo $form->labelEx($model,'username'); ?>
        <?php echo $form->textField($model,'username',array('size'=>60,'maxlength'=>255)); ?>
        <?php echo $form->error($model,'username'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'password'); ?>
        <?php echo $form->passwordField($model,'password',array('size'=>60,'maxlength'=>255)); ?>
        <?php echo $form->error($model,'password'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'firstname'); ?>
        <?php echo $form->textField($model,'firstname',array('size'=>60,'maxlength'=>255)); ?>
        <?php echo $form->error($model,'firstname'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($model,'lastname'); ?>
        <?php echo $form->textField($model,'lastname',array('size'=>60,'maxlength'=>255)); ?>
        <?php echo $form->error($model,'lastname'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx(Email::model(),'emailaddress'); ?>
    <?php echo $form->textField(Email::model(),'emailaddress',array('size'=>60,'maxlength'=>255)); ?>
    <?php echo $form->error(Email::model(),'emailaddress'); ?>
    </div>

    <div class="row">
                <?php echo $form->labelEx($model,'fer_roles_id'); ?>
                <?php
                        echo $form->dropDownList($model, 'fer_roles_id', 
                        CHtml::listData(Roles::model()->findAll(), 'id', 'description'),
                        array('class' => 'my-drop-down', 'options' => array('2' => array('selected' => "selected")

                                    )
                                )
                            );
                ?> 
    </div>

    <div class="row buttons">
        <?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
    </div>

<?php $this->endWidget(); ?>

</div>

you could do this: Controller:

<?php

public function actionCreate()
{
    $model = new Users;
    $model1 = new Email;

    $roles = Roles::model()->findAll();

    if(isset($_POST['Users']))
    {
        $model->attributes = $_POST['Users'];
        $model->password = crypt($model->password, 'salt');
        $model->datecreated = new CDbExpression('NOW()');
        $model->save();

        $model1->attributes = $_POST['Users']['emailaddress'];
        $model1->fer_users_id = $model->id;
        $model1->save();

        $this->redirect(array('view', 'id' => $model->id));
    }

    $this->render('create', array(
        'user'=>$model,
        'email'=>$model1,
        'roles'=> $roles
    ));
}

?>

your view:

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'users-form',
    'enableAjaxValidation'=>false,
)); ?>

    <p class="note">Fields with <span class="required">*</span> are required.</p>

    <?php echo $form->errorSummary($user); ?>

    <div class="row">
        <?php echo $form->labelEx($user,'username'); ?>
        <?php echo $form->textField($user,'username',array('size'=>60,'maxlength'=>255)); ?>
        <?php echo $form->error($user,'username'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($user,'password'); ?>
        <?php echo $form->passwordField($user,'password',array('size'=>60,'maxlength'=>255)); ?>
        <?php echo $form->error($user,'password'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($user,'firstname'); ?>
        <?php echo $form->textField($user,'firstname',array('size'=>60,'maxlength'=>255)); ?>
        <?php echo $form->error($user,'firstname'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($user,'lastname'); ?>
        <?php echo $form->textField($user,'lastname',array('size'=>60,'maxlength'=>255)); ?>
        <?php echo $form->error($user,'lastname'); ?>
    </div>

    <div class="row">
        <?php echo $form->labelEx($email,'emailaddress'); ?>
    <?php echo $form->textField($email,'emailaddress',array('size'=>60,'maxlength'=>255)); ?>
    <?php echo $form->error($email,'emailaddress'); ?>
    </div>

    <div class="row">
                <?php echo $form->labelEx($user,'fer_roles_id'); ?>
                <?php
                        echo $form->dropDownList($user, 'fer_roles_id', 
                        CHtml::listData($roles, 'id', 'description'),
                        array('class' => 'my-drop-down', 'options' => array('2' => array('selected' => "selected")

                                    )
                                )
                            );
                ?> 
    </div>

    <div class="row buttons">
        <?php echo CHtml::submitButton($user->isNewRecord ? 'Create' : 'Save'); ?>
    </div>

<?php $this->endWidget(); ?>

</div>

if you get any error with this, let me know..