Yii2ActiveRecordsave () method Problems

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.