The data method of the ThinkPHPCURD method is used to set the value of the data object to be operated. This article mainly introduces the application of the data method. For more information, see
The data method of the ThinkPHP CURD method is used to set the value of the data object to be operated. This article mainly introduces the application of the data method. For more information, see
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. 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 ();