yii2 - 为什么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 ????????????
解决方案
您的模型有错误,要查找问题所在以及哪些属性有错误,请更改此:
$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 **/
}
推荐阅读
- c# - 为 Microsoft echo bot 创建内存测试服务器
- ios - 多上下文核心数据:从 childContext 中删除对象
- postgresql - 在 JPA 中使用 Flyway 创建的序列
- ios - Apollo iOS swift 总是从本地缓存中获取 GraphQl
- .net-core - 在 Autofac 中注册回调并在回调中再次构建容器
- asp.net - 为什么无论是否选中复选框,剑道验证都不会让我通过
- c# - 如何解决在IIS中调试的问题
- mysql - MySQL 查询的等效 MemSQL 查询
- c# - 在 mvc core 2.1.3 API 中使用 FormatFilter 属性
- sql-server - 选择重复值