The framework sometimes uses the contents of the database, as mentioned in the essay "Thinkphp Framework Knowledge", which is now described in detail in this essay.
I. LINK database
(1) Locate the Conf folder in the module folder and then write the config.php file
I'm here for this file path
(2) Open the config.php file, and then locate the parent class profile convention.php file, copy and paste the section about "database" into the config.php configuration file
| 123456789 |
/* 数据库设置 */ ‘DB_TYPE‘=> ‘‘, // 数据库类型 ‘DB_HOST‘=> ‘‘, // 服务器地址 ‘DB_NAME‘=> ‘‘, // 数据库名 ‘DB_USER‘=> ‘‘, // 用户名 ‘DB_PWD‘=> ‘‘, // 密码 ‘DB_PORT‘=> ‘‘, // 端口 ‘DB_PREFIX‘=> ‘‘, // 数据库表前缀 ‘DB_FIELDS_CACHE‘=> true, // 启用字段缓存(开发时这个要写成false) |
Here is the contents of my database connection
| 1234567891011121314 |
<?php returnarray( //‘配置项‘=>‘配置值‘ /* 数据库设置 */ ‘DB_TYPE‘=> ‘mysql‘, // 数据库类型 ‘DB_HOST‘=> ‘localhost‘, // 服务器地址 ‘DB_NAME‘=> ‘test3‘, // 数据库名 ‘DB_USER‘=> ‘root‘, // 用户名 ‘DB_PWD‘=> ‘123‘, // 密码 ‘DB_PORT‘ => ‘3306‘, // 端口 ‘DB_PREFIX‘=> ‘‘, // 数据库表前缀 ‘DB_FIELDS_CACHE‘ => false, // 启用字段缓存(开发时这个要是false)); |
After the connection is successful, the new model file is created.
Second, the new model file (specific content see thinkphp framework Knowledge)
(1) Locate the Model folder in the Module folder and create a new one in this folder
A) The model itself is a class file
b) each data table in the database corresponds to a model file
c) The simplest data model class
Your own model file
| 1234567 |
<?phpnamespaceHome\Model; //命名空间的写法use Think\Model; //使用模型class InfoModel extends Model{ } |
(2) Look at this data model
We can then write the control file in the Controller folder, here is a control file, I use this.
Open the control file and write a method called Ceshi ().
| 123456 |
publicfunction ceshi(){ $info = new \Home\Model\InfoModel(); //造新对象,这里是Info模型 var_dump($info); //输出一下,看下结果 } |
Output look at the results, note the Address bar here (in the framework of knowledge has been introduced in the 4 access mode):
This is my path:
The following is the corresponding model data:
Third, the operation of the database (the following is the Thinkphp model basic class provides a "coherent operation method" coherent operation method)
You can use the method in this control file to write directly in this method.
| 123456 |
publicfunction ceshi(){ $info = new \Home\Model\InfoModel(); //造新对象,这里是Info模型 //下面是数据库操作 } |
(1) Querying all data in a table (method: Select ())
Returns a two-dimensional array (associated)
The returned data where the field name is lowercase, so as to make the database as small as possible
For example:
| 12 |
$arr= $info->select();var_dump($arr); |
See if all the contents of the database are queried
Content in the database:
(2) Querying a single piece of data (method: Find ())
For example:
| 1 |
$arr= $info->find("p002");<br>var_dump($arr); |
This is the query: The code name is "P002" information, as follows:
The Select () method can also query one or more data, as follows:
| 12 |
$arr= $info->select("p001,p002");var_dump($arr); |
This is the query: The code name is "P001" and "P002" information, as follows:
(3) Conditional query data (method: where ())
| 1 |
$arr= $info->where("code=‘p003‘")->select();<br>var_dump($arr); |
This is the condition of the query: The code name is p003 all information
(4) Switch data table (method: Table ())
For example:
| 1 |
$arr= $info->table("nation")->select();<br>var_dump($arr); |
This is the change of a nation table:
(5) Select the field of Operation (method: Field ())
For example:
| 1 |
$arr= $info->field("name,sex,birthday")->select();<br>var_dump($arr); |
Query fields are: Name, gender, and birthday fields
(6) Sort the data (method: Order ())
For example:
| 12 |
$arr= $info->order("code desc")->select();var_dump($arr); |
This is the descending order of code
(7) Paging query data (method: Limit () and page ())
Example: Limit ()
| 12 |
$arr= $info->limit("2,2")->select();var_dump($arr); |
This is the content of the 2nd page that is displayed:
Example: page ()
| 12 |
$arr= $info->page("2,2")->select();var_dump($arr); |
This is also the content of the 2nd page that is displayed:
Attention:
The difference between limit () and page (): the latter parameter is the direct display of "page number", "several"
(8) Grouping data (method: Table ())
For example:
| 12 |
$arr= $info->table("car")->field("max(price)")->group("brand")->select();var_dump($arr); |
This is the maximum price in the car table of the query and is grouped according to the field of brand
For example:
| 12 |
$arr= $info->table("car")->field("max(price)")->group("brand")->having("max(price)>60")->select();var_dump($arr); |
This is the query above, but adds a condition that the price is greater than 60
(9) Link Query Database (method: Join ())
For example:
| 12 |
$arr= $info->field("info.code as ‘代号‘, info.name as ‘姓名‘,nation.name as ‘民族‘")->join("nation on info.nation=nation.code")->select();var_dump($arr); |
This is the two tables of the link query (where the last character is used in Chinese, because his value is used later)
(10) de-weight (distinct ())
For example:
| 12 |
$arr= $info->table("car")->distinct(true)->field("brand")->select();var_dump($arr); |
This is a duplicate field in the queried database
The above is the framework of the coherent operation of the commonly used to several, there is also the beginning of how to link the database
Go
TP Framework Knowledge (link database and operational data)