TP Framework Knowledge (link database and operational data)

Source: Internet
Author: User

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

/* Database Settings */    ' db_type ' + '  ,     //database type    ' db_host ' = ' + ',//  server address    ' db_name '               = =  ',          //Database name    ' db_user ' + ', '//      user name    ' db_pwd '                = '  ,          //password    ' db_port ' + '  ,        //Port    ' db_prefix ' = ', '/    /database table prefix    ' Db_fields_cache '       +  true,        //Enable field caching (this should be written as false when developing)

Here is the contents of my database connection

<?php return Array (//' configuration item ' = ' config value '/* Database settings */' db_type '               = '  mysql ',     //database type    ' db_host '               = >  ' localhost ',//server address    ' db_name '               = '  test3 ',          //Database name    ' db_user  ' + ' Root ',//      username    ' db_pwd '                = '  123 ',          //password    ' db_port '               = '  3306 ',        / /Port    ' db_prefix ' + '  ,       //database table prefix ' db_fields_cache '       =  false,    //enable field cache ( If this is false at the time of development);

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

<?phpnamespace Home\model;   The name space is written with the use Think\model;          Use 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 ().

Public Function Ceshi () {$info = new \home\model\infomodel ();  Create new object, here is Info model var_dump ($info);  Output, look at the results}

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.

Public Function Ceshi () {$info = new \home\model\infomodel ();  Create new object, here is the info model//Below is the database operation}

 

(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:

$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:

$arr = $info->find ("P002");
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:

$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 ())

$arr = $info->where ("code= ' p003 '")->select ();
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:

$arr = $info->table ("Nation")->select ();
Var_dump ($arr);

This is the change of a nation table:

(5) Select the field of Operation (method: Field ())

For example:

$arr = $info->field ("Name,sex,birthday")->select ();
Var_dump ($arr);

Query fields are: Name, gender, and birthday fields

(6) Sort the data (method: Order ())

For example:

$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 ()

$arr = $info->limit ("2,2")->select (); Var_dump ($arr);

This is the content of the 2nd page that is displayed:

Example: page ()

$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:

$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:

$arr = $info->table ("Car")->field ("Max")->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:

$arr = $info->field ("Info.code as ' codename ', Info.name as ' name ', nation.name as ' People '")->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:

$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 method commonly used to several, there are beginning how to link the database, the following continue to update in ~ ~ ~

TP Framework Knowledge (link database and operational data)

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.