Providing you have your models setup properly:
User(id, username, password)
Profile(user_id, first_name, last_name, gender)
form:
PHP Code:
<form action="path/to/controller" method="post">
<fieldset>
<legend>Account Information</legend>
<p>
<?php echo CHtml::label($user, 'username'); ?>
<?php echo CHtml::activeTextField($user, 'username'); ?>
</p>
<p>
<?php echo CHtml::label($user, 'password'); ?>
<?php echo CHtml::activePasswordField($user, 'password'); ?>
</p>
</fildset>
<fieldset>
<legend>Profile Information</legend>
<p>
<?php echo CHtml::label($profile, 'first_name'); ?>
<?php echo CHtml::activeTextField($profile, 'first_name'); ?>
</p>
<p>
<?php echo CHtml::label($profile, 'last_name'); ?>
<?php echo CHtml::activeTextField($profile, 'last_name'); ?>
</p>
<p>
<?php echo CHtml::label($profile, 'gender'); ?>
<?php echo CHtml::activeDropDownList($profile, 'gender', array('prompt'=>'Select a gender:','m'=>'Male', 'f'=>'Female')); ?>
</p>
</fildset>
<p>
<input type="submit" value="Save">
</p>
</form>
controller action
PHP Code:
public function actionSignup()
{
$user = new User;
$profile = new Profile;
// gets the current request
$request = Yii::app()->getRequest();
// if User && Profile fields were submitted via a post request
if(($userAttributes = $request->getPost('User')) !== null && ($profileAttributes = $request->getPost('Profile')) !== null)
{
// assigns the values to the model properties
$user->attributes = $userAttributes;
$profile->attributes = $profileAttributes;
// begin a transaction
$transaction = $user->->dbConnection->beginTransaction();
try
{
// save models and commit transaction
$user->save();
$profile->user_id = $user->id;
$profile->save();
$transaction->commit();
}catch(Exception $e)
{
// transaction fails, error is stored in $e->getMessage();
$transaction->rollBack();
}
// render form view...
}
}