Summarize thinkphp experience Sharing (iv)

Source: Internet
Author: User

Summary of Thinkphp's curd easy to ignore
1, do not define the method, directly render the template.
For the operation method without any actual logic, we just need to define the corresponding template file directly, such as the form page, this page usually does not have variables to output to the template, so we do not have to write a corresponding empty method and then $this->display ().
2. Introduction of Create method.
Assuming that the model we instantiate is $model, then thinkphp can add data to the database directly through $model->add (), so if we call $model->create () before $model->add () method, so what is the point? The Create () method has only one meaning of "ensure that data written to the database is secure and valid."
The automatic validation of data is achieved by using the Create method. It is worth mentioning that using the Create method for automatic validation requires that we define the model **model.class.php and then instantiate it using the D () method.
3. Insert the data using the object's method.
You may often insert data in the following way (array mode) $Form = D (' Form '); $data [' title '] = ' thinkphp '; $data [' content '] = ' form contents '; $Form->add ($data);
In fact, thinkphp also supports the object to insert data directly into the database, as follows: $Form = D (' Form '); $Form->title = ' thinkphp '; $Form->content = ' form content '; $Form->add ();
4, do not specify the conditions for data updates. $Form = M ("Form"); The data object property to be modified is assigned a value $data [' id '] = 5; $data [' title '] = ' thinkphp '; $data [' content '] = ' ThinkPHP3.1 release ';  $Form->save ($data); Save modified data based on conditions
The Save method automatically recognizes the primary key field in the data object and acts as an update condition. Of course, you can also explicitly pass in the update condition, which is our most commonly used method: $Form = M ("Form"); The data object property to be modified is assigned a value $data [' title '] = ' thinkphp '; $data [' content '] = ' ThinkPHP3.1 release '; $Form->where (' id=5 ')->save ($data); Save modified data based on conditions
In fact there is the way of the object, as mentioned above, inserting the data, the same way as the object: $Form = M ("Form"); The data object property to be modified is assigned a value $Form->title = ' thinkphp '; $Form->content = ' ThinkPHP3.1 version released '; $Form->where (' id=5 ')->save (); Save modified data based on conditions
Also, you can include the primary key field in the data you want to save so that you don't have to write where $Form = M ("Form"); The value of the data object property to be modified is assigned $Form->id = 5; $Form->title = ' thinkphp '; $Form->content = ' ThinkPHP3.1 version released '; $Form->save (); Saves modified data based on the primary key in the data object
5. Modification of individual field values.
Sometimes we just need to modify the value of a field to use the SetField method without having to call the Save method every time. $Form = M ("Form"); Change the title Value $Form->where (' id=5 ')->setfield (' title ', ' thinkphp ');
6. Powerful Increment/decrement field value operation.
Thinkphp, you can perform a direct or decrement operation on a field's data.
For statistical fields, the system also provides a more convenient method for Setinc and Setdec. $User = M ("User"); Instantiate the User object $User->where (' id=5 ')->setinc (' Score ', 3); The user's points plus 3 $User->where (' id=5 ')->setinc (' score '); The user's points plus 1 $User->where (' id=5 ')->setdec (' Score ', 5); User's points minus 5 $User->where (' id=5 ')->setdec (' score '); User's points minus 1
7, do not use where to perform the deletion.
$User->delete (' 1,2,5 '); Delete the user data for primary keys 1, 2, and 5.
This usage can be used in an example of an auto-increment of user points, similar to the sign-in time.
8, $this->assign () method of the use of single parametersIn Thinkphp's Handbook, there is the use of the Assign () method, which is no longer described here. And what we're going to talk about is that the Assign method if only one parameter is used. IndexAction.class.php

  1. <? PHP
  2. Class indexaction extends Action {
  3. public function index() {
  4. $var _array = array("Color" = " Blue",
  5. "Size" = "Medium",
  6. "Shape" = "Sphere");
  7. $this-Assign($var _array);
  8. $this, display();
  9. }
  10. }
Copy Code

?> index.html

    1. {$color} <!--here output blue-->
Copy Code

As can be seen from the above example, the framework assigns each key-value pair in the parameter array to the template as "parameter" = "value", so if you need to assign a value to a template with a lot of values, you might want to pay attention to this way: Since this is implicitly assigned, you should pay attention to the name of the parameter! Especially the fields that are commonly used in databases such as ID and name

Summarize thinkphp experience Sharing (iv)

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.