TAGS :Viewed: 39 - Published at: a few seconds ago

[ Yii mysql transaction: data saving only in one table ]

I am very new to Yii. I was trying something like this--

  public function actionCreate()

{
    $a=new Tblcompany;
            $b= new Tblusergroup;

  $transaction= Yii::app()->db->beginTransaction();

try{
    if(isset($_POST['Tblcompany']))
    {

                    $a->attributes=$_POST['Tblcompany'];

        if($a->save()){

         $b->company_code= $a->company_code;

                  $b->usergroup_code='Admin';

                  $b->save();

                      $transaction->commit();


            $this->redirect(array('view','id'=>$a->company_code));

            }
    }

    $this->render('create',array(
    'a'=>$a,
            'b'=>$b,
    ));

 }catch(Exception $error){

  $transaction->rollback();
    }

}

don't know why no data is being saved in tblusergroup. Although data is perfectly added in tblcompany..Kindly help please :/

Answer 1


Probably your $b model failed validation.

Put

var_dump($b->getErrors());
die();

just after $b->save(); and see the errors. It must be empty array in order to save the model to DB.