Table of Contents
【数据库操作model模型】
【创建模型】
【数据库使用步骤】:
【数据库配置config.php】:
【从数据库获得相关信息—查询】
【大小写问题】:
【通过model模型获得数据具体演示】:
【实例化模型方式】
  【 创建一个模型,有可能这个模型对应的表前缀与其他表前缀不一致,那么这个模型就需要定义自己的真实表名字】:
?
Home Backend Development PHP Tutorial Thinkphp入门 5 —模型 (49)

Thinkphp入门 5 —模型 (49)

Jun 13, 2016 pm 12:21 PM
model nbsp select

Thinkphp入门 五 —模型 (49)

【数据库操作model模型】

model  模型  数据库操作

tp框架主要设计模式:MVC

C:controller   控制器   shop/Lib/Action/具体控制器

V:view       视图     shop/Tpl/分组/模板文件

M:model      数据模型  shop/Lib/Model/具体模型

 

【创建模型】

创建的原则:一个数据表对应一个数据模型

创建模型:

当出现以下信息,说明我们的数据库没有配置用户名和密码config.php

 

【数据库使用步骤】:

  1. 创建模型model文件shop/Lib/Model/GoodsModel.class.php
  2. 在config.php里边设置数据库的库名、用户名、密码、表前置信息
  3. 在控制器里边可以直接实例化GoodsModel模型对象

【数据库配置config.php】:

【从数据库获得相关信息—查询】

我们使用select()方法从数据库获得数据信息,该方法返回一个二维数组,

如果我们想要获得一条记录,就使用find()方法,该方法返回一维数组

 

$goods  ->  select()   //获得全部记录、全部字段结果

$goods  ->  select(15)  //获得主键等于15 的记录结果

$goods  ->  select(“10,15,20”)  //获得主键在10、15、20范围内的记录信息

 

$goods  ->  find(16);   //以一维数组的形式给我们返回主键等于16的记录信息

 

$goods  ->  getByXXX();  //根据具体XXX字段做where条件进行查询,一维数组结果返回。该方法会走Model.class.php 里边的__call()自动条件方法。

【大小写问题】:

【通过model模型获得数据具体演示】:

【实例化模型方式】

  1. 传统方式new  GoodsModel()就可以
  2. 可以同过快捷函数D()来实例化模型对象

例如: D(‘Goods’);  实例化Goods模型对象

               D();   实例化基类对象,该对象在使用的时候需要指定“数据表名”

                            和主键名字,较复杂,不推荐直接使用

  3 通过快捷函数M()实例化基类对象

例如:M()  实例化Model.class.php对应的对象,该方法不推荐使用

 

  【 创建一个模型,有可能这个模型对应的表前缀与其他表前缀不一致,那么这个模型就需要定义自己的真实表名字】:

  

?

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution: Your organization requires you to change your PIN Solution: Your organization requires you to change your PIN Oct 04, 2023 pm 05:45 PM

Solution: Your organization requires you to change your PIN

How to adjust window border settings on Windows 11: Change color and size How to adjust window border settings on Windows 11: Change color and size Sep 22, 2023 am 11:37 AM

How to adjust window border settings on Windows 11: Change color and size

How to change title bar color on Windows 11? How to change title bar color on Windows 11? Sep 14, 2023 pm 03:33 PM

How to change title bar color on Windows 11?

OOBELANGUAGE Error Problems in Windows 11/10 Repair OOBELANGUAGE Error Problems in Windows 11/10 Repair Jul 16, 2023 pm 03:29 PM

OOBELANGUAGE Error Problems in Windows 11/10 Repair

How to enable or disable taskbar thumbnail previews on Windows 11 How to enable or disable taskbar thumbnail previews on Windows 11 Sep 15, 2023 pm 03:57 PM

How to enable or disable taskbar thumbnail previews on Windows 11

Display scaling guide on Windows 11 Display scaling guide on Windows 11 Sep 19, 2023 pm 06:45 PM

Display scaling guide on Windows 11

10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

10 Ways to Adjust Brightness on Windows 11

How to turn off private browsing authentication for iPhone in Safari? How to turn off private browsing authentication for iPhone in Safari? Nov 29, 2023 pm 11:21 PM

How to turn off private browsing authentication for iPhone in Safari?

See all articles