thinkphp Explanation (v)--database configuration and model data models layer, query

Source: Internet
Author: User
Tags php class

Database configuration

In the TP framework to connect to the database operation, to be configured

To find the "database Configuration" in convertion.php and copy it to the Project Profile config.php

Model Layer Production     

model: one for each table in the database
The class name is the name of the table, and the member variable inside the class is the column name
A table corresponds to a class where one data corresponds to an object

If we don't have a special operation on the model of the table, we don't have to build it.

Now we're going to make one of the simplest model models InfoModel.class.php

<?phpnamespace Home\model;  Namespaces use Think\model under the Model folder in home;     Parent class file in thinkphp/library/think/model.class.php class Infomodel extends model{}

  

Three instances of model mode

Instantiate the Model class    //new namespace \model\-name ()//$info = new \home\model\infomodel ();  Var_dump ($info);//$info =d ("info");   D ("model Mark")//var_dump ($info); $car =m ("car");   M ("Data sheet mark");//var_dump ($car);

  

Querying the database

Var_dump ($car->select ());//query, return data of two-dimensional array of the situation//$attr = $car->where ("brand= ' b002 '")->select ();// The Where method can add a query condition//$attr = $car->table ("Nation")->select ();//table method toggles the action table//$attr = $car->field ("Code,name") ->select ();//field can specify the field of the query//$attr = $car->order ("Oil desc")->select ();//Sort/$attr = $car->limit (2,2)- >select ();//paged query, if a parameter n represents the first n, if the two parameter m,n means skip the first m, take N. $attr = $car->page (3,2)->select ();//paging, two parameters m,n for page m, n data//$attr = $car->field ("brand,count (*)") Group ("brand")->select ();//Group Query//select * FROM info join nation on condition;//$attr = $car->join ("Brand on car.brand= Brand.brand_code ")->select ();//Connection query//$attr = $car->distinct (true)->field (" brand ")->select ();//go to re-query// $attr = $car->find ("c001"), a single-dimensional array is returned based on the primary key, the primary key value is not written, the first//$attr = $car->select ("c001,c002") is returned by default, and/or based on the primary key value query, Returns a two-dimensional array $attr= $car->where ("name like '% Audi% '")->order ("Powers desc")->select (); $this->assign ("Shuzu", $                  attr); $this->display ()

  

thinkphp Explanation (v)--database configuration and model data models layer, query

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.