Data method in thinkphp

Source: Internet
Author: User

The data method is also one of the consistent operation methods of the model class, used to set the value of the object currently being manipulated, and perhaps we are not accustomed to using this method today to explain how to use the good data method.

Usage

Write operations

$Model = D (' User ');
$Model->create ();
//Here to skip the specific automatic generation and verification of the Judgment
$Model->add ();

or assign a value directly to the data object, for example:

$Model = M (' User ');
$Model->name = ' fleeting ';
$Model->email = ' [email protected] ';
$Model->add ();

The data method, then, is to directly generate the object to manipulate, for example:

$Model = M (' User ');
$data [' name '] = ' fleeting ';
$data [' email '] = ' [email protected] ';
$Model->data ($data)->add ();

Note: If we use both the Create method and the data object, then the method that is called later is valid.

the data method supports arrays, objects, and strings in the following way:

$Model = M (' User ');
$obj = new StdClass;
$obj->name = ' fleeting ';
$obj->email = ' [email protected] ';
$Model->data ($obj)->add ();

Here's how to use strings:

$Model = M (' User ');
$data = ' name= fleeting &[email protected] ';
$Model->data ($data)->add ();

You can also add data directly to the Add method by passing in the data object, for example:

$Model = M (' User ');
$data [' name '] = ' fleeting ';
$data [' email '] = ' [email protected] ';
$Model->add ($data);

But this way the data parameter can only use arrays.

of course, the data method can also be used to update information, such as:

$Model = M (' User ');
$data [' id '] = 8;
$data [' name '] = ' fleeting ';
$data [' email '] = ' [email protected] ';
$Model->data ($data)->save ();

Of course we can also use this directly:

$Model = M (' User ');
$data [' id '] = 8;
$data [' name '] = ' fleeting ';
$data [' email '] = ' [email protected] ';
$Model->save ($data);

Similarly, the data parameter can only pass in the array at this time.

when the Save method is called to update the data, it automatically determines if there is a primary key value in the current data object, and if so, automatically updates the condition. In other words, the following usage is equivalent to the above:

$Model = M (' User ');
$data [' name '] = ' fleeting ';
$data [' email '] = ' [email protected] ';
$Model->data ($data)->where (' id=8 ')->save ();

Read operation

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

$User = M (' User ');
$map [' name '] = ' fleeting ';
$User->where ($map)->find ();
Reads the current data object
$data = $User->data ();

Data method in thinkphp

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.