Update Two Models With One View Not Working In Yii?

Can anybody help me resolve my issue? I’m able to do create new records but I can’t modify or update my existing records.

Here is my source code:


public function actionCreate() {

    $model = new Branchmaster;

    $user = new Usermaster;

    if(isset($_POST['Branchmaster'], $_POST['Usermaster'])) {

      $model->attributes=$_POST['Branchmaster'];

      $user->attributes=$_POST['Usermaster'];

      $valid=$model->validate();

      $valid=$user->validate() && $valid;

      if($valid){

         $model->save();

         $chnuser->save();                   

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

      }

    }

    $this->render('create',array(

      'model'=>$model,

      'user'=>$user,

    ));

  }


  public function actionUpdate($id) {

    $model=$this->loadModel($id);

    $user = Usermaster::model()->findByAttributes(array('branch_id'=>$model->Id));

    if(isset($_POST['Branchmaster'], $_POST['Usermaster'])) {

      $model->attributes=$_POST['Branchmaster'];

      $user->attributes=$_POST['Usermaster'];


      $valid=$model->validate();

      $valid=$user->validate() && $valid;

      if($valid){

        $model->save();

        $user->save();

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

      }

    }

    $this->render('update',array(

      'model'=>$model,

      'user'=>$user,

    ));

  }

[twitter]rohisuthar[/twitter]

Hi,

Are you getting any error?

your code should work… its look good. but you should show what exact output did u get and expected?

In function create you have to link $chnuser to the $model.




public function actionCreate() {

    $model = new Branchmaster;

    $user = new Usermaster;

    if(isset($_POST['Branchmaster'], $_POST['Usermaster'])) {

      $model->attributes=$_POST['Branchmaster'];

      $user->attributes=$_POST['Usermaster'];

      $valid=$model->validate();

      $valid=$user->validate() && $valid;

      if($valid){

         $model->save();

         $chnuser->branch_id=$model->id;      ///THIS LINE             

         $chnuser->save();                   

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

      }

    }

    $this->render('create',array(

      'model'=>$model,

      'user'=>$user,

    ));

  }