Update Two Models Of Different Databases With Single View In Yii

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.


Your approach is right because you are updating objects in two different tables, but if you are also using two different databases you have to override the method getBdConnection() inside your model.