1-----Data Access
2-----Data Query
3-----Data Additions
4-----Data modification
5-----Data Deletion
Create a Nation table and write three test data
Create TableNation (' Code 'int( the) not NULLauto_increment, ' name 'varchar( -) not NULL, Primary Key(' code '));Insert into' Nation 'Values(1,'Human race'),(2,'God Clan'),(3,'Demon Clan'),(4,'Human race');
One data access (m in MVC)
-- models should be built under the model file
--for example, to operate on a nation table
--Create NationModel.class.php under the Model folder to create that table, which table is used to name the model, and create the class Nationmodel and file names that inherit from model
--Modify the database link configuration and copy the database settings from tp/thinkphp/conf/convention.php to tp/application/home/conf/config.php
Modify the library settings, modify ' Db_fields_cache ' in the respective module and false//enable field cache to false at development time
①-- First Type: new mode
<? phpnamespace Home\controller; use Think\controller; class TestController extends Controller { public function Test () { $conn = new \home\model\nationmodel (); var_dump ( $conn ); // output and view } // }
②--Second type: D () method
<? phpnamespace Home\controller; Use Think\controller; Public function Test () { $conn = D ("Nation"); write the table name directly within the method var_dump($conn);} // sub-class object Nationmodel}
③--the Third Way: M () method
<? phpnamespace Home\controller; Use Think\controller; Public function Test () { $conn = M ("Nation"); write the table name directly within the method var_dump($conn);} // Parent Class object Model}
Three ways to differentiate
--The first new method must write the model file
--the second method, if the model exists, creates the subclass object and, if it does not exist, creates the parent class object
--the third method of creating the parent class object
--Recommended to use the 23rd method
blog from the "Million", Address: Blog from the "Million", Address: http://www.cnblogs.com/wannian/p/8999946.html
Query for two data (written in the test () method)
The--TP framework comes with a method of accessing the nation table as an example. $conn as a Table object);
|--returns the data
--$conn->select (); Query, and returns a two-dimensional array,
--if there is no value, the query is all, if the value written to the primary key returns the corresponding query result, multiple values are separated by commas
--$conn->find (' primary key value ') queries an array, returning an array
--$conn->count (); Query and return the number of data bars,
--$conn->max (' field name '); Query and return the largest data value in a field
--$conn->min (' field name '); Query and return the smallest data value in a field
--$conn->avg (' field name '); Returns the average of the field data, four digits after the decimal point
--$conn->sum (' field name '); Returns the and of the field data
|--Other: coherent operation
--field (""); The column to be queried, the value of the field name, or the count () method, such as: $conn->field (' Code ')->select (); Querying data for the Code column
--where (""), based on conditional query, syntax as with SQL statement, for example: $conn->where ("code= ' 1 ' or code= ' 2 '")->select (); Query by condition
--table (""); Temporary switch table, only on this line switch for example: $conn->table (' user ')->select (); Switch to the ' user ' table and query
--alias (""); Setting aliases for tables for example: $conn->alias ("T1")->select ();
--order (""); Sort for example: $conn->order (' Code desc ')->select (); Sort by the Code column in descending order
--limit (""); Limit the number of query bars for example: $conn->limit ("")->select (); Skip 1 data, fetch 2 data;
--page ("page, article"); Paging Query For example: $conn->page ("2,2")->select (); Take the 2nd page of data, each page shows 2 articles
--group (""); Packet queries such as: $conn->group ("name")->field ("Name,count (*)")->select (); Query name and count by Name field
--having (""); Mate Group Query filter criteria for example: $conn->group ("name")->having ("Count (name) >1")->select (); Group by name and query for data with more than 1 bars
--distinct (""); De-weight For example: $conn->field ("name")->distinct ("name")->select (); Query the Name column and go back to the
--lock (true); Add a lock For example: $conn->lock (True)->select (); While executing this statement, other people are unable to operate, (the error operation can be avoided when the access data is very concentrated in a large number of times)
--join (); SQL statements are recommended for even table queries, which are considered better than this method.
|--sql Statement Query
$conn->query (""); To write SQL statements in the query method, it is recommended to use the
The addition of three data
① using arrays with the Add () method
Public function Other () {$conn = D ('nation); $arr = Array (' Code ' = ' 5 ', ' name ' = ' elf clan '); Define an associative array, index corresponding field name, value corresponding to the value of the data $conn->add ($arr); Add (); method adds a value;}
② using the $conn object with the Add () method (because $conn is an object and has the code and name two attributes, assign a value to it)
Public function Other () {$conn = D ('nation); $conn->code=' 6 '; $conn->name=' orc clan '; $conn->add ();}
③ Automatic collection of forms
--Front code
--The path submitted to its own method
The value of--name is the field name of the database table
<formAction= "__self__"Method= "POST"> <Div><inputtype= "text"name= "Code"></Div> <Div><inputtype= "text"name= "Name"></Div> <Div><inputtype= "Submit"value= "Login"></Div></form>
--code for the model
--Determine whether to submit
--create () method collects form data automatically
--add () add
Public function Other () { $conn = D ("Nation"); if (empty($_post)) { $this-Show (); } Else { // perform add operation $conn->create (); Auto-collect form $connAdd ();} }
Modification of four data
① using the array with the Save () method
Public function Other () {$conn = D ('nation); $arr = Array (' Code ' = ' 7 ', ' name ' = ' goblin clan '); $conn->save ($arr); Save () modify;}
② using the $conn object with the Add () method
Public function Other () {$conn = D ('nation); $conn->code=' 6 '; $conn->name=' Day Wing clan '; $conn->save ();}
③ Automatic collection of forms
--Front code
--The path submitted to its own method
The value of--name is the field name of the database table
<formAction= "__self__"Method= "POST"> <Div><inputtype= "text"name= "Code"></Div> <Div><inputtype= "text"name= "Name"></Div> <Div><inputtype= "Submit"value= "Login"></Div></form>
--code for the model
public function other () { $conn = D ("Nation" ); if (empty ( $_post $this ->show (); else { // Perform add action $conn ->create (); // auto-collect form $conn ->save (); }}
Five Data deletion
|--delete () method value is the value of the primary key;
|--execute () method, with a value of SQL statement, to be added and censored
tinkphp Framework Learning-02 Model classes