The data method of ThinkPHP CURD

Source: Internet
Author: User

The data method of the ThinkPHP CURD method is also one of the coherent operation methods of the model class. It is used to set the value of the data object to be operated currently, but many developers are not very familiar with this method, today, I will explain how to use the data method well.

The usage is as follows:

1. Write operation

Generally, we generate data objects by using the create method or value assignment method and write them to the database. For example:

$ Model = D ('user'); $ Model-> create (); // The specific Automatic Generation and verification methods are skipped here. $ Model-> add ();

Or assign values to data objects directly, for example:

$ Model = M ('user'); $ Model-> name = 'Year'; $ Model-> email = 'thinkphp @ qq.com '; $ Model-> add ();

The data method directly generates the data object to be operated, for example:

$ Model = M ('user'); $ data ['name'] = 'current year'; $ data ['email '] = 'thinkphp @ qq.com '; $ Model-> data ($ data)-> add ();

NOTE: If we use the create method and data to create data objects at the same time, the method called later is valid.

The data method supports arrays, objects, and strings., The object method is as follows:

$ Model = M ('user'); $ obj = new stdClass; $ obj-> name = 'current year'; $ obj-> email = 'thinkphp @ qq.com '; $ Model-> data ($ obj)-> add ();

The string method is as follows:

$ Model = M ('user'); $ data = 'name = current year & email = thinkphp@qq.com '; $ Model-> data ($ data)-> add ();

You can also directly input a data object in the add method to add data. For example:

$ Model = M ('user'); $ data ['name'] = 'current year'; $ data ['email '] = 'thinkphp @ qq.com '; $ Model-> add ($ data );

However, in this way, the data parameter can only use arrays.

Of course, the data method can also be used to update data, for example:

$ Model = M ('user'); $ data ['id'] = 8; $ data ['name'] = 'Year '; $ data ['email '] = 'thinkphp @ qq.com'; $ Model-> data ($ data)-> save ();

Of course, we can also use it like this:

$ Model = M ('user'); $ data ['id'] = 8; $ data ['name'] = 'Year '; $ data ['email '] = 'thinkphp @ qq.com'; $ Model-> save ($ data );

Similarly, the data parameter can only be input into an array.

When you call the save method to update data, the system automatically determines whether a primary key value exists in the current data object. If yes, it is automatically used as the update condition. That is to say, the following usage is equivalent to the above:

$ Model = M ('user'); $ data ['name'] = 'current year'; $ data ['email '] = 'thinkphp @ qq.com '; $ Model-> data ($ data)-> where ('Id = 8')-> save ();

2. Read operations

In addition to write operations, the data method can also be used to read the current data object, for example:

$ User = M ('user'); $ map ['name'] = 'current year'; $ User-> where ($ map)-> find (); // read the current data object $ data = $ User-> data ();

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.