简体   繁体   English

在yii中用单个视图更新不同数据库的两个模型

[英]Update two models of different databases with single view in yii

I have a view (_form.php) with fields (name,summary) submit button. 我有一个具有字段(名称,摘要)提交按钮的视图(_form.php)。 If I click on submit button, it should update Name field of One model and Summary field of another model.Both this models are of different databases. 如果我单击提交按钮,它将更新一个模型的名称字段和另一个模型的摘要字段。这两个模型都属于不同的数据库。 Can anyone help on this. 任何人都可以帮忙。 I tried the following for this 我为此尝试了以下
In _form.php(Test) _form.php(测试)

<?php echo $form->labelEx($model, ‘name’); ?>
<?php echo $form->textField($model, ‘name’, array(‘size’ => 60, ‘maxlength’ => 250)); ?>
<?php echo $form->error($model, ‘name’); ?>
<?php echo $form->labelEx(Test1::model(), ‘summary’); ?>
<?php echo $form->textField(Test1::model(), ‘summary’, array(‘size’ => 60, ‘maxlength’ => 250)); ?>
<?php echo $form->error(Test1::model(), ‘summary’); ?>
<?php echo CHtml::submitButton($model->isNewRecord ? ‘Create’ : ‘Save’); ?>

In TestController.php TestController.php中

public function actionCreate() {
        $model = new Test;
        if (isset($_POST['Test'])) {
            $model->attributes = $_POST['Test'];
            if ($model->save()) {
                $modeltest1 = new Test1;
                $modeltest1->attributes = $_POST['Test1'];
                $modeltest1->Id = $model->Id;
                if ($modeltest1->save())
                    $this->redirect(array('view', 'Id' => $model->Id));
            }
        }
        $this->render('create', array(
            'model' => $model,
        ));
    }  

This code is not working. 该代码不起作用。 How can I make it work for different databases. 如何使它适用于不同的数据库。 I followed the below link for this. 我为此点击了以下链接。

http://www.yiiframework.com/wiki/291/update-two-models-with-one-view/

This code actually should work, but its bad. 这段代码实际上应该可以工作,但是不好。

I assume that you dont understand at all what is model and what its doing in Yii, also how to render and create forms. 我假设您根本不了解什么是模型及其在Yii中的功能,以及如何渲染和创建表单。

I'll try to explain how it should be. 我将尝试解释它应该如何。

1st of all dont use Test::model() in views, unless you want to call some function from it(but try to avoid it). 首先,不要在视图中使用Test::model() ,除非您想从中调用某个函数(但要避免使用它)。 It can be done by passing it from controller: 可以通过从控制器传递它来完成:

    public function actionCreate() {
            $model_name = new Name;
            $model_summary=new Summary;
//something here
            $this->render('create', array(
                'name' => $model_name,
                'summary'=>$model_summary,
            ));
        }  

When you make render you passing variables to your view (name_in_view=>$variable) 进行渲染时,将变量传递到视图(name_in_view => $ variable)

2nd. 2号 In your view you can use your variables. 在您看来,您可以使用变量。

<?php echo $form->labelEx($name, ‘name’);
echo $form->textField($name, ‘name’, array(‘size’ => 60, ‘maxlength’ => 250));
echo $form->error($name, ‘name’);
echo $form->labelEx($summary, ‘summary’);
echo $form->textField($summary, ‘summary’, array(‘size’ => 60, ‘maxlength’ => 250)); ?>
echo $form->error($summary, ‘summary’); ?>
echo CHtml::submitButton($model->isNewRecord ? ‘Create’ : ‘Save’); ?>

3rd. 第三名 You need to understand what is model. 您需要了解什么是模型。 It's class that extends CActiveRecord in this case. 在这种情况下,它是扩展CActiveRecord的类。 Your code in controller should loo like: 您在控制器中的代码应如下所示:

public function actionCreate() {
                $model_name = new Name;
                $model_summary=new Summary;
                if (isset($_POST['Name']))
                   $model_name->attributes=$_POST['Name'];
                if (isset($_POST['Summary']))
                   $model_name->attributes=$_POST['Summary'];
                if ($model_name->save()&&$model_summary->save())
                   $this->redirect(array('view', 'Id' => $model->Id));  
                $this->render('create', array(
                    'name' => $model_name,
                    'summary'=>$model_summary,
                ));
            }

$model->attributes=$_POST[] here is mass assignment of attributes, so they must be safe in rules. $model->attributes=$_POST[]这是属性的批量分配,因此它们在规则中必须安全。 You always can assign attributes with your hands (1 by 1), or form an array and push it from array. 您总是可以用手分配属性(1比1),也可以形成一个数组并将其从数组中推入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM