RT, which had been thought before
ActiveRecord->save 方法
Can be used when the data does not exist
insert
, when present
update
, and later saw this document on the Civil Service Web site:
// 新建一条记录$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()) { // 获取用户输入的数据,验证并保存}
According to this logic, if I want to update id = 100
This data information now, if the data table does not have this record, then, so, you $model === null
have to judge, if empty, instantiate one $model
, and then:
$model = new Customer();$model->id = 100;.....
Do you feel that it's not elegant enough or that I ActiveRecord
have a problem with my understanding?
Reply content:
RT, previously thought ActiveRecord->save 方法
can be when the data does not exist, when insert
there update
, later on the civil service online to see this document:
// 新建一条记录$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()) { // 获取用户输入的数据,验证并保存}
According to this logic, if I want to update id = 100
This data information now, if the data table does not have this record, then, so, you $model === null
have to judge, if empty, instantiate one $model
, and then:
$model = new Customer();$model->id = 100;.....
Do you feel that it's not elegant enough or that I ActiveRecord
have a problem with my understanding?
Yii Chinese official website answer
save
is based on the primary key of the table, if you pass in the primary key is the update, or vice versa is to add