首页 > 解决方案 > 为什么yii2中的'id'等于null

问题描述

控制器

保存后id等于batchInsert中的null

$model= new Sale();
  
if($model->load(Yii::$app->request->post())) {
      $model->status=1;
      $model->price = str_replace(",","",$model->price);
      //$id = $model->id        
      $model->save();
      $id = $model->id; 
      if($model->pardakht == 1) {
          for($i=0;$i<$model->count;$i++) {
                $data[$i][0]=Yii::$app->request->post('number_check')[$i];
                $data[$i][1]=Yii::$app->request->post('price_check')[$i];
                $data[$i][2]=Yii::$app->request->post('date_check')[$i];
                $data[$i][3]=Yii::$id;
                $data[$i][4]=Yii::$model->customer_id;
          }               

          Yii::$app->db->creatCommand()->batchInsert('sale_check', [
              'number_check',
              'price_check',
              'date_check',
              'user_id',
              'customer_id'
          ], $data)->execute();
     }
}

var_dump($id);

$id = null ????????????

标签: yii2

解决方案


您的模型有错误,要查找问题所在以及哪些属性有错误,请更改此:

$model->save();
$id = $model->id; 

if($model->save()) {
    $id = $model->id;
    /** other codes ****/
}else{
    var_dump($model->errors);
    /** or other usage of this array result **/
}

推荐阅读