Thinkphp入门 5 —模型 (49)
Thinkphp入门 五 —模型 (49)
【数据库操作model模型】
model 模型 数据库操作
tp框架主要设计模式:MVC
C:controller 控制器 shop/Lib/Action/具体控制器
V:view 视图 shop/Tpl/分组/模板文件
M:model 数据模型 shop/Lib/Model/具体模型
【创建模型】
创建的原则:一个数据表对应一个数据模型
创建模型:
当出现以下信息,说明我们的数据库没有配置用户名和密码config.php
【数据库使用步骤】:
- 创建模型model文件shop/Lib/Model/GoodsModel.class.php
- 在config.php里边设置数据库的库名、用户名、密码、表前置信息
- 在控制器里边可以直接实例化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模型获得数据具体演示】:
【实例化模型方式】
- 传统方式new GoodsModel()就可以
- 可以同过快捷函数D()来实例化模型对象
例如: D(‘Goods’); 实例化Goods模型对象
D(); 实例化基类对象,该对象在使用的时候需要指定“数据表名”
和主键名字,较复杂,不推荐直接使用
3 通过快捷函数M()实例化基类对象
例如:M() 实例化Model.class.php对应的对象,该方法不推荐使用
【 创建一个模型,有可能这个模型对应的表前缀与其他表前缀不一致,那么这个模型就需要定义自己的真实表名字】:
?

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Solution: Your organization requires you to change your PIN

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

How to change title bar color on Windows 11?

OOBELANGUAGE Error Problems in Windows 11/10 Repair

How to enable or disable taskbar thumbnail previews on Windows 11

10 Ways to Adjust Brightness on Windows 11

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