doukuiqian5345 2014-04-19 03:51
浏览 39

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>
  • 写回答

1条回答

  • douyi3307 2014-04-19 19:23
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划