RT,之前一直以为 ActiveRecord->save 方法
可以当数据不存在时 insert
,存在时update
,后来在中文官网上看到了这段文档:
// 新建一条记录
$model = new Customer;
if ($model->load(Yii::$app->request->post()) && $model->save()) {
// 获取用户输入的数据,验证并保存
}
// 更新主键为$id的AR
$model = Customer::findOne($id);
if ($model === null) {
throw new NotFoundHttpException;
}
if ($model->load(Yii::$app->request->post()) && $model->save()) {
// 获取用户输入的数据,验证并保存
}
按照这个逻辑,我现在如果想更新id = 100
这条数据信息,如果数据表中没有这条记录,那么$model === null
,如此一来,还得先判断,如果为空,实例化一个 $model
,然后:
$model = new Customer(); $model->id = 100;.....
感觉这样不够优雅,还是说我对ActiveRecord
的理解有问题?
哈士奇WWW
繁花如伊