The role of model object $origin object in thinkphp frame

Source: Internet
Author: User

In the model constructor, the $origin is initialized in the following way

if (Is_object ($data)) {      $this->data = Get_object_vars ($data);} else {      $this->data = $data;} Record raw Data $this->origin = $this->data;

$origin save an array, because if you save an object, because the object is a reference, the modification will change the $data and $origin at the same time.

The effect we need is to $origin the original data at the time of the initial initialization.

In Thinkphp's save function, you can compare $data and $origin to find out which fields have been modified so that you can update only the modified fields when you update them.

$data = Array_udiff_assoc ($this->data, $this->origin, function ($a, $b) {       if (empty ($a) | | empty ($b)) &&am P $a!== $b) {              return 1;       }       return Is_object ($a) | | $a! = $b? 1:0;});

The role of model object $origin object in thinkphp frame

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.