简体   繁体   中英

yii handsontable can't save

i was want to save my data from handsontable. Nothings error when i try to save my data. but my data from handsontable cannot insert in my database. Here is my code

public function actionSavePricelist($id)
{
    header("Content-Type: application/json");

    if(!Yii::app()->request->isPostRequest)
        throw new CHttpException(400,'Invalid request. Please do not repeat this request again.');

    $user = Yii::app()->session['loginSession']['userId'];

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

        $vendor_price= json_decode($_POST['price_list']);
        $flagError=0;
        for($i=0;$i<count($vendor_price->data);$i++) {
            $row = $vendor_price->data[$i];
            if($row[0] == null && $row[1] == null) continue;
            else { 
                if(!isset($row[3])){
                    $mVenPri = new MstVendorPricelist;
                    $idcity = MstCity::model()->findByAttributes(array(
                            'city_name' => $row[1]
                        )
                    );
                    $idc = $idcity->city_id;

                    $mVenPri->item_id=$row[3];
                    $mVenPri->vendor_id=$model->vendor_id;
                    $mVenPri->city_id=$idc;
                    $mVenPri->price=$row[2];
                    $mVenPri->created_date=date('Y-m-d H:i:s');
                    $mVenPri->created_by=$user;
                    $mVenPri->modified_date=date('Y-m-d H:i:s');
                    $mVenPri->modified_by=$user;

                    if(!$mVenPri->save())
                        $flagError++;

                } else {
                    $modelprice=MstVendorPriceList::model()->findByPk($id);
                    $idcity = MstCity::model()->findByAttributes(array(
                        'city_name' => $row[1])
                        );
                    $idc = $idcity->city_id;

                    $modelprice->item_id=$row[3];
                    $modelprice->vendor_id=$model->vendor_id;
                    $modelprice->city_id=$idc;
                    $modelprice->price=$row[2];
                    $modelprice->created_date=date('Y-m-d H:i:s');
                    $modelprice->created_by=$user;
                    $modelprice->modified_date=date('Y-m-d H:i:s');
                    $modelprice->modified_by=$user;

                    if(!$modelprice->save())
                        $flagError++;
                }                               
            }               
        } 
        if($flagError == 0)
            echo CJSON::encode(array('success'=>true,'msg'=>'You have successfully added data.'));
        else
            echo CJSON::encode(array('msg'=>'Error occurred during inserting details.'));
        // var_dump($vendor_price);
}

actually this function is update but,similar like save data. Update my vendor and save new data from handsontable. please help me. is there any wrong with my code

It's looks like, what before save validation in MstVendorPricelist model, return false. You should find out it in model code. Ensure that all fields get a correct data, when you fill it in controller.

Also you can try disable validations when inserting data. read this

 if(!$mVenPri->save(false))
        $flagError++; 

You find more information about Active Record in this guide

Happy coding!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM