Home > Backend Development > PHP Tutorial > Getting Started with Thinkphp 5—Model (49), thinkphp49_PHP Tutorial

Getting Started with Thinkphp 5—Model (49), thinkphp49_PHP Tutorial

WBOY
Release: 2016-07-13 09:51:03
Original
1098 people have browsed it

Introduction to Thinkphp 5 - Model (49), thinkphp49

[Database operation model model]

model model database operation

The main design pattern of tp framework: MVC

C:controller shop/Lib/Action/specific controller

V:view shop/Tpl/group/template file

M: model Data model shop/Lib/Model/Specific model

【Create model】

Principle of creation: one data table corresponds to one data model

Create model:

When the following message appears, it means that our database does not have a username and password configured in config.php

[Database usage steps]:

[Database configuration config.php]:

[Obtain relevant information from the database—Query]

We use the select() method to obtain data information from the database, which returns a two-dimensional array,

If we want to get a record, use the find() method, which returns a one-dimensional array

$goods -> select() //Get all records and all field results

$goods -> select(15) //Get the record results with the primary key equal to 15

$goods -> select(“10,15,20”) //Get the record information with the primary key within the range of 10, 15, 20

$goods -> find(16); //Return to us the record information with the primary key equal to 16 in the form of a one-dimensional array

$goods -> getByXXX(); //Query based on the where condition of the specific XXX field, and the one-dimensional array result is returned. This method will use the __call() automatic condition method in Model.class.php.

【Capitalization issue】:

[Detailed demonstration of obtaining data through the model]:

【Instancing model method】

For example: D(‘Goods’); Instantiate the Goods model object

D(); Instantiate the base class object. When using this object, you need to specify the "data table name"

and primary key names are more complicated and are not recommended to be used directly

 3 Instantiate the base class object through the shortcut function M()

For example: M() instantiates the object corresponding to Model.class.php. This method is not recommended

 【Create a model, it is possible that the table prefix corresponding to this model is inconsistent with other table prefixes, then this model needs to define its own real table name】:

 

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1015275.htmlTechArticleThinkphp Getting Started Five - Model (49), thinkphp49 [Database Operation Model Model] model model database operation tp framework main design Mode: MVC C: controller controller shop/Lib/...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template