thinkphp model Layer

Source: Internet
Author: User

Thinkphp want to do model, first change the configuration file: in the home folder under the Conf config file to modify:

We can copy the convention inside. Modify:

<?PHPreturnArray (//' Config item ' = ' config value '    'Show_page_trace'=true,    /*Database Settings*/    'Db_type'='MySQL',//Database Type    'Db_host'='localhost',//Server Address    'db_name'=' Book',//Database name    'Db_user'='Root',//User name    'db_pwd'="',//Password    'Db_port'="',//Port    'Db_prefix'="',//database table Prefixes    'Db_params'= = Array (),//Database Connection Parameters    'Db_debug'= TRUE,//SQL log can be logged when database debug mode is turned on    'Db_fields_cache'=false,//Enable field caching    'Db_charset'='UTF8',//database encoding is UTF8 by default    'Db_deploy_type'=0,//Database Deployment method: 0 Centralized (single server), 1 distributed (Master-slave server)    'db_rw_separate'=false,//whether the database reads and writes separate master-slave valid    'Db_master_num'=1,//number of master servers for read-write separation    'Db_slave_no'="',//Specify the number from the server    /*Data Cache Settings*/);

Change the database name to your own, and we'll start creating the Model object:

<?PHPnamespaceHome\controller;//Home is the root folder, the controller is the folder where the file residesUse Think\controller;//think is the root folder, the controller is the folder where the file residesclassMaincontroller extends controller{ Publicfunction Index () {$n= D ("Leibie");//Create a Model object, and if you create a model file, use this$n = M ("Leibie");//creating a Model object, creating a parent class object$arr = Array ("Lbid"="b005","Lbname"="Fiction class"); //add by using array mode$nAdd ($arr); }            }

We create a file in the home model LEIBIEMODEL.CLASSP.HP:

<? PHP namespace Home\model;use Think\model; class Leibiemodel extends model{}

We created the model object to look up data from the database:

<?PHPnamespaceHome\controller;//Home is the root folder, the controller is the folder where the file residesUse Think\controller;//think is the root folder, the controller is the folder where the file residesclassMaincontroller extends controller{ Publicfunction Index () {//$n = D ("Leibie");//Create a Model object, and if you create a model file, use this$n = M ("Leibie");//creating a Model object, creating a parent class object//$arr = Array ("lbid" = "b005", "Lbname" and "Fiction class"); //ways to get Data$arr = $nSelect();//Read all dataVar_dump ($arr); }            }

The output is:

This is all the data in the Leibie table.

<?PHPnamespaceHome\controller;//Home is the root folder, the controller is the folder where the file residesUse Think\controller;//think is the root folder, the controller is the folder where the file residesclassMaincontroller extends controller{ Publicfunction Index () {//$n = D ("Leibie");//Create a Model object, and if you create a model file, use this$n = M ("Leibie");//creating a Model object, creating a parent class object//$arr = Array ("lbid" = "b005", "Lbname" and "Fiction class"); //ways to get Data$arr = $n->find ("b001");//read a piece of dataVar_dump ($arr); }            }

Read the output of a single piece of data:

And look at the total number of returned data:

<?PHPnamespaceHome\controller;//Home is the root folder, the controller is the folder where the file residesUse Think\controller;//think is the root folder, the controller is the folder where the file residesclassMaincontroller extends controller{ Publicfunction Index () {//$n = D ("Leibie");//Create a Model object, and if you create a model file, use this$n = M ("Leibie");//creating a Model object, creating a parent class object//$arr = Array ("lbid" = "b005", "Lbname" and "Fiction class"); //ways to get Data$arr = $n->count ();//returns the total number of data for this tableVar_dump ($arr); }            }

The output is:

Thinkphp also supports coherent operation, conditional query:

//consistent Operation//$arr = $n->where ("price>50")->select ();//Add Query Conditions//$arr = $n->table ("brand")->select ();//Toggle Data Table//$arr = $n->field ("Code,name")->select ();//determining which columns to find//$arr = $n->order ("Price")->select ();//Sort//$arr = $n->limit (3,3)->select ();//paging Query, skip 3 strip Fetch 3//$arr = $n->page (2)->select ();//take the second page//$arr = $n->join ("")->select ();//Connection Query//$arr = $n->query ();//querying native SQL statements

Here is not one of them, we can try.

thinkphp model Layer

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.