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

时间:2014-04-19 03:51:03

标签: php yii frameworks

我非常喜欢PHP和Yii框架。

单击此处查看我的SQL数据库设置示例http://i.stack.imgur.com/W6qQj.png

我为所有三个表设置了控制器和模型,并为Gii为Users表设置了CRUD视图。我已经读完了这个 - > http://www.yiiframework.com/wiki/19/how-to-use-a-single-form-to-collect-data-for-two-or-more-models/并尝试了多组代码示例等......但都失败了。我能做的最好的事情是获取创建表单,将所有数据插入到users表中,而不是电子邮件表中。我相信我的简短来自我的UsersController.php - > public function actionCreate()部分。有人可以用一段示例代码帮助我将信息发布到电子邮件表中吗?

--- --- EDIT 如下所述,请求信息......

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

这是users / _form.php视图文件:

<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>

1 个答案:

答案 0 :(得分:2)

你可以这样做: 控制器:

<?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
    ));
}

?>

你的观点:

<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>

如果您对此有任何错误,请与我们联系。