php - Yii,CactiveForm 在数据库中保存多个表

标签 php mysql sql yii

我是 yii 的新手,我正在尝试将数据保存在 CactiveForm 上的不同表中..这是我查看的代码:

<?php
/* @var $this UserController */
/* @var $user User */
/* @var $form CActiveForm */
?>

<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
        'id'=>'user-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,
)); ?>

        <p class="note">Fields with <span class="required">*</span> are required.</p>

        <?php echo $form->errorSummary($user, $gunwcuser, $game, $cash); ?>

        <div class="row">
                <?php echo $form->labelEx($user,'user'); ?>
                <?php echo $form->textField($user,'user',array('size'=>16,'maxlength'=>16)); ?>
                <?php echo $form->error($user,'user'); ?>
        </div>

        <div class="row">
                <?php echo $form->labelEx($user,'Gender'); ?>
                <?php echo $form->radioButtonList($user,'Gender', array(0 => 'Male', 1 => 'Female'), array('labelOptions'=>array('style'=>'display:inline'), 'separator'=>' ',)); ?>
                <?php echo $form->error($user,'Gender'); ?>
        </div><br>

        <div class="row">
                <?php echo $form->labelEx($user,'NickName'); ?>
                <?php echo $form->textField($user,'NickName',array('size'=>16,'maxlength'=>16)); ?>
                <?php echo $form->error($user,'NickName'); ?>
        </div>

        <div class="row">
                <?php echo $form->labelEx($user,'Password'); ?>
                <?php echo $form->passwordField($user,'Password',array('size'=>16,'maxlength'=>16)); ?>
                <?php echo $form->error($user,'Password'); ?>
        </div>

        <div class="row">
                <?php echo $form->labelEx($user,'E_Mail'); ?>
                <?php echo $form->textField($user,'E_Mail',array('size'=>50,'maxlength'=>50)); ?>
                <?php echo $form->error($user,'E_Mail'); ?>
        </div>

        <div class="row">
                <?php echo $form->labelEx($user,'Country'); ?>
                <?php echo $form->textField($user,'Country',array('size'=>3,'maxlength'=>3)); ?>
                <?php echo $form->error($user,'Country'); ?>
        </div>

        <div class="row buttons">
                <?php echo CHtml::submitButton($user->isNewRecord ? 'Create' : 'Save'); ?>
        </div>

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

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

数据库中有4个表..分别是(user、gunwcuser、game、cash)。此表单用于用户表,gunwcuser 将具有与用户表单中放入的相同数据,但并非请求保存在表单上的所有数据。

这是我的 Controller :

public function actionCreate()
        {
                $user = new User;
                $gunwcuser =new Gunwcuser;
                $game = new Game;
                $cash = new Cash;

                // Uncomment the following line if AJAX validation is needed
                // $this->performAjaxValidation($model);

                $auth = 1;
                $time = '0000-00-00 00:00:00';

                if(isset($_POST['User']))
                {


                        // Set data column in DB before saving
                        $user->Status = '1';
                        $user->MuteTime = $time;
                        $user->RestrictTime = $time;
                        $user->Authority = $auth;
                        $user->User_Level = '1';
                        $user->Authority2 = $auth;
                        $user->attributes=$_POST['User'];

                        $_POST['Gunwcuser'] = $_POST['User'];
                        $gunwcuser->Status = '1';
                        $gunwcuser->MuteTime = $time;
                        $gunwcuser->RestrictTime = $time;
                        $gunwcuser->Authority = $auth;
                        $gunwcuser->User_Level = '1';
                        $gunwcuser->Authority2 = $auth;
                        $gunwcuser->AuthorityBackup = $auth;
                        $gunwcuser->attributes=$_POST['Gunwcuser'];

                        $_POST['Game'] = $_POST['User'];



                        if($user->save())
                                $this->redirect(array('view','id'=>$user->Id));
                }

                $this->render('create',array(
                        'user'=>$user, 'gunwcuser'=>$gunwcuser, 'game'=>$game, 'cash'=>$cash,
                ));
        }

我尝试将属性从用户复制到gunwcuser,但是当我保存用户的数据时,我检查数据库以查看gunwcuser是否也已保存,但事实并非如此。

游戏 table 上还有一些数据是相同的,但大部分数据会有所不同,与现金相同..

常用数据有(ID、用户名、昵称、性别、电子邮件、国家/地区、密码)..

谁能告诉我如何在同一个表单的不同表中保存数据?还同时保存不同的数据

最佳答案

嗯..让我们看看。您有两个模型,但只保存了一个:)。添加此代码

$gunwcuser ->save()

在$this->重定向之前

如果你想同时保存 $user 和 $gunwcuser,你必须在每个模型对象上使用 save() 方法

if($user->save() && $gunwcuser ->save())

    $this->redirect(array('view','id'=>$user->Id));

关于php - Yii,CactiveForm 在数据库中保存多个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20060510/

相关文章:

php - 使用 Symfony 从 Doctrine 模型创建数据库

mysql在where子句中拆分字符串

c# - 执行标量();用 scope_identity() 生成 "System.InvalidCastException: Specified cast is not valid"

java - 空指针异常

php - Laravel Eloquent 错误地更新每一行值

java - 在php中将字符串转换为utf-8

php - 列表中带有特殊字符的 Mysql like 语句无法搜索

PHP获取mysql表信息函数

mysql - 按数据库中的 Emp_ID 和信用日期插入最短日志时间和最长日志时间

php - 获取数组中除最后一个之外的所有值