3

我对 Yii 框架很陌生,我想从几个模型中创建一个表单。我有两个名为用户和配置文件的表格,表格必须包含两个表格列。这是我在视图中创建的表单:

<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'),
    );
}

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

4

1 回答 1

3

按照这个维基它会指导你做你需要的

于 2013-09-13T05:35:45.863 回答