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>
duanhe6718
duanhe6718 抱歉,您的模型可能会出现错误,请检查您的模型规则
6 年多之前 回复
dongxing2692
dongxing2692 只是一个注意事项我试图扭转FK,看看我是否可以让代码工作,没有运气......只是在这一点上尝试任何事情!
6 年多之前 回复
dongpai1942
dongpai1942 还上传你的动作控制器代码..
6 年多之前 回复
douke6027
douke6027 请上传您的查看代码...
6 年多之前 回复
dongyiyu882684
dongyiyu882684 模型的名称是“用户”,“电子邮件”和“角色”。
6 年多之前 回复
dongsi2317
dongsi2317 你必须首先制作模型的实例,然后将属性添加到模型中,之后你必须保存..模型的名称是什么?
6 年多之前 回复

1个回答

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

doutan1857
doutan1857 很遗憾,它无法识别电子邮件地址并继续生成此错误... i.stack.imgur.com/4svWU.png
6 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐