RT. Previously, I thought that the ActiveRecord-& amp; gt; save method can be used to insert data when the data does not exist and update data when the data exists. Later I saw this document on the Chinese official website: {code ...} according to this logic, if I want to update the id100 data information now, if the data table does not contain... RT, I always thought
ActiveRecord-> save MethodIf the data does not exist
insert, Exists
updateLater, I saw this document on the Chinese official website:
// Create a new record $ model = new Customer; if ($ model-> load (Yii ::$ app-> request-> post ()) & $ model-> save () {// obtain user input data, verify and save} // update the AR $ model with the primary key of $ id = Customer :: findOne ($ id); if ($ model = null) {throw new NotFoundHttpException;} if ($ model-> load (Yii :: $ app-> request-> post () & $ model-> save () {// obtain user input data, verify and save}
According to this logic, if I want to updateid = 100This data information. If the data table does not contain this record$model === nullIn this way, you must first judge. If it is null, instantiate$modelAnd then:
$model = new Customer();$model->id = 100;.....
I feel this is not elegant enough.ActiveRecordIs there a problem with understanding?
Reply content:
RT, I always thoughtActiveRecord-> save MethodIf the data does not existinsert, ExistsupdateLater, I saw this document on the Chinese official website:
// Create a new record $ model = new Customer; if ($ model-> load (Yii ::$ app-> request-> post ()) & $ model-> save () {// obtain user input data, verify and save} // update the AR $ model with the primary key of $ id = Customer :: findOne ($ id); if ($ model = null) {throw new NotFoundHttpException;} if ($ model-> load (Yii :: $ app-> request-> post () & $ model-> save () {// obtain user input data, verify and save}
According to this logic, if I want to updateid = 100This data information. If the data table does not contain this record$model === nullIn this way, you must first judge. If it is null, instantiate$modelAnd then:
$model = new Customer();$model->id = 100;.....
I feel this is not elegant enough.ActiveRecordIs there a problem with understanding?
Answers to Yii Chinese official website
saveIt is based on the table's primary key. If you pass in a primary key, it is an update, and vice versa.