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