Some tips and tricks for using thinkphp (IV.)

Source: Internet
Author: User
Keywords thinkphp
Tags based content create data display file key modified

A summary of the curd of thinkphp

1, does not define the method, directly renders the template.

For the operation without any actual logic, we just need to directly define the corresponding template file on the line, such as Form page, this page will not have variables to the template output, so we do not need to write a corresponding empty method and then $this->display ().

2. Introduction to create method.

Assuming that our instantiated model is $model, then thinkphp can add data directly to the database by $model->add (), so if we call $model->create () before $model->add () method, what's the point? The Create () method has only one meaning to ensure that data written to the database is safe and valid.

The automatic verification of data is realized by means of the Create method. It is worth mentioning that using the Create method to automate validation requires that we define the model **model.class.php and then instantiate it using the D () method.

3. Insert data using object method.

You may often insert data in the following way (array)
$Form = D (' Form ');
$data [' title '] = ' thinkphp ';
$data [' content '] = ' form contents ';
$Form->add ($data);

In fact, thinkphp also supports objects by inserting data directly into the database, as follows:
$Form = D (' Form ');
$Form->title = ' thinkphp ';
$Form->content = ' form content ';
$Form->add ();

4. Do not specify the condition to update the data.
$Form = M ("Form"); Data object property assignment to modify
$data [' id '] = 5;
$data [' title '] = ' thinkphp ';
$data [' content '] = ' ThinkPHP3.1 release ';
$Form->save ($data); Save modified data based on criteria

The Save method automatically recognizes the primary key field in the data object and serves as an update condition. Of course, you can also explicitly pass in the update condition, which is our most common method:
$Form = M ("Form");
Data object property assignment to modify
$data [' title '] = ' thinkphp ';
$data [' content '] = ' ThinkPHP3.1 release ';
$Form->where (' id=5 ')->save ($data); Save modified data based on criteria

There are also objects, as mentioned above, inserting data in the same way as objects:
$Form = M ("Form");
Data object property assignment to modify
$Form->title = ' thinkphp ';
$Form->content = ' ThinkPHP3.1 release ';
$Form->where (' id=5 ')->save (); Save modified data based on criteria

Also, you can include the primary key field in the data that you want to save so that you don't need to write where
$Form = M ("Form");
Data object property assignment to modify
$Form->id = 5;
$Form->title = ' thinkphp ';
$Form->content = ' ThinkPHP3.1 release ';
$Form->save (); Save modified data based on the primary key in the data object

Related Article

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.