Update two models of different databases with single view in yii

1.2k views Asked by At

I have a view (_form.php) with fields (name,summary) submit button. 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)

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

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

There are 1 answers

1
ineersa On

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.

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

2nd. 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. 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. You always can assign attributes with your hands (1 by 1), or form an array and push it from array.