Yii Framework Forum: Update Two Models With One View Not Working In Yii? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Update Two Models With One View Not Working In Yii?

#1 User is offline   Rohit Suthar 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 202
  • Joined: 12-May 14
  • Location:Mumbai, India

Posted 26 May 2014 - 03:20 AM

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,
    ));
  }



@rohisuthar
Thanks & regards,

ROHIT SUTHAR,
Mumbai(India)

Follow me - @rohisuthar

www.yiiframework.us



Don't be shellfish to click +1 button
0

#2 User is offline   chandran 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 495
  • Joined: 08-October 12
  • Location:Austria

Posted 26 May 2014 - 04:27 AM

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?
Thanks
chandran nepolean

My own extension:
http://www.yiiframew...sion/ejqueryte/

My wiki
http://www.yiiframew...checkboxcolumn/

If it work dont hesitate to click +1 button
0

#3 User is offline   rahif 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 178
  • Joined: 11-July 13

Posted 27 May 2014 - 03:53 AM

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,
    ));
  }

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users