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

Getting Started with Thinkphp 5—Model (49)

WBOY
Release: 2016-08-08 09:22:47
Original
1050 people have browsed it

【Database operation model model】

model model Database operation

tp framework main design pattern: MVC

C: controller controller shop/Lib/Action/specific controller

V: view 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 , indicating that our database does not have a username and password configured in config.php

[Database usage steps]:

  1. Create the model file shop/Lib/Model/GoodsModel.class.php
  2. Set it in config.php The library name, user name, password, and table pre-information of the database
  3. You can directly instantiate the GoodsModel model object in the controller

[Database configuration config.php]:

[Get 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 obtain a record, we 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, and 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 model model】:

【Instancing model method】

  1. The traditional way is new GoodsModel()
  2. You can instantiate the model object through the shortcut function D(). For example: D('Goods'); Instantiate the Goods model object
"Data table name"

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

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

For example: M() Instantiate 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】:

?

The above introduces Getting Started with Thinkphp 5 - Model (49), including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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