在Yii中从多个模型创建表单

时间:2013-09-13 00:44:25

标签: php forms yii

我对Yii框架很新,我想从几个模型创建一个表单。我有两个名为users和profiles的表,表单必须包含两个表列。这是我在视图中创建的表单:

<div class="form">

    <?php
    $form = $this->beginWidget('CActiveForm', array(
        'id' => 'users-form',
        // Please note: When you enable ajax validation, make sure the corresponding
        // controller action is handling ajax validation correctly.
        // There is a call to performAjaxValidation() commented in generated controller code.
        // See class documentation of CActiveForm for details on this.
        'enableAjaxValidation' => false,
            ));
    ?>

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

    <div class="row">
        <div class="span6 ">
            <?php echo $form->labelEx($model, 'firstname'); ?>
            <?php echo $form->textField($model, 'firstname', array('size' => 50, 'maxlength' => 50, 'class' => 'input-block-level')); ?>
            <?php echo $form->error($model, 'firstname'); ?>
        </div>
        <div class="span6">
            <?php echo $form->labelEx($model, 'lastname'); ?>
            <?php echo $form->passwordField($model, 'lastname', array('size' => 50, 'maxlength' => 50, 'class' => 'input-block-level')); ?>
            <?php echo $form->error($model, 'lastname'); ?>
        </div>
    </div>

    <div class="row">
        <div class="span6 ">
            <?php echo $form->labelEx($model, 'username'); ?>
            <?php echo $form->textField($model, 'username', array('size' => 30, 'maxlength' => 30, 'class' => 'input-block-level')); ?>
            <?php echo $form->error($model, 'username'); ?>
        </div>
        <div class="span6">
            <?php echo $form->labelEx($model, 'password'); ?>
            <?php echo $form->passwordField($model, 'password', array('size' => 60, 'maxlength' => 80, 'class' => 'input-block-level')); ?>
            <?php echo $form->error($model, 'password'); ?>
        </div>
    </div>

    <div class="row">
        <div class="span6">
            <?php echo $form->labelEx($model, 'email'); ?>
            <?php echo $form->textField($model, 'email', array('size' => 60, 'maxlength' => 100, 'class' => 'input-block-level')); ?>
            <?php echo $form->error($model, 'email'); ?>
        </div>
        <div class="span6">
            <?php echo $form->labelEx($model, 'deactive'); ?>
            <?php $accountStatus = array(0 => 'فعال', 1 => 'غیر فعال'); ?>
            <div class="radio-block"><?php echo $form->radioButtonList($model, 'deactive', $accountStatus, array('separator' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;')); ?></div>
            <?php echo $form->error($model, 'deactive'); ?>
        </div>
    </div>

    <div class="row buttons">
        <?php echo CHtml::submitButton($model->isNewRecord ? 'ایجاد کاربر' : 'Save', array('class' => 'btn btn-info pull-left')); ?>
    </div>

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

</div><!-- form -->

我的模型还包括两个表之间的关系:

public function relations()
{
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array('profiles' => array(self::HAS_ONE, 'Profiles', 'userid'),
)

public function relations()
{
    // NOTE: you may need to adjust the relation name and the related
    // class name for the relations automatically generated below.
    return array(
        'user' => array(self::BELONGS_TO, 'Users', 'userid'),
    );
}

如何处理我的视图以同时将其连接到两个模型?

1 个答案:

答案 0 :(得分:3)

Follow this wiki 它会引导你做你需要的事情