【问题标题】:yii handsontable can't saveyii掌上电脑无法保存
【发布时间】:2017-05-23 06:29:33
【问题描述】:

我想从handsontable 中保存我的数据。当我尝试保存我的数据时没有任何错误。但是我来自handsontable的数据无法插入我的数据库。 这是我的代码

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

其实这个功能是更新但是,类似于保存数据。更新我的供应商并保存来自handsontable 的新数据。 请帮我。我的代码有什么问题吗

【问题讨论】:

    标签: php yii handsontable


    【解决方案1】:

    看起来,在MstVendorPricelist模型中保存验证之前,返回false。您应该在模型代码中找到它。当您在控制器中填写数据时,请确保所有字段都获得正确的数据。

    您也可以在插入数据时尝试禁用验证。 read this

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

    您可以在此guide 中找到有关 Active Record 的更多信息

    编码愉快!

    【讨论】:

      猜你喜欢
      • 2013-02-11
      • 2011-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-08
      • 1970-01-01
      • 2014-04-13
      相关资源
      最近更新 更多