ThinkPHP的Model操作

几种调用模型的方法:

use think\Controller;

//use app\index\model\App;
use think\Loader;

class Index extends Controller
{
public function index()
{
//$res = App::get(2); 【第一种】
//$app = new App; 【第二种】
$app = Loader::model(‘App‘); 【第三种】
$res = $app->get(4);
$res = $res->toArray();
dump($res);
}
}

使用模型查询数据的方法
【获取一条记录】 get方法和find方法
public function index()
{
$res = App::get(function($query){
$query->where(‘id‘,‘eq‘,3)->field(‘name, status‘);
});

// $res = App::where(‘name‘,‘eq‘,‘ipad‘)->field(‘name, key‘)->find();
$res = $res->toArray();
dump($res);

}

【获取多条数据】【all】

public function index()
{

// $res =App::all(‘1,2,3‘); 【字符串形式】
//$res = App::all([2,3,4]); //【数组形式】
$res = App::all(function($query){
$query->where(‘id‘,‘<‘,5)->field(‘name,key,status‘);
});
foreach($res as $val){
$res = $val->toArray();
dump($res);
}

}

where方法直接获取

public function index()
{
// $res = App::where(‘id‘,‘<‘,5)->select();
// foreach($res as $val){
// $res = $val->toArray();
// dump($res);
// }
$res = App::where(‘id‘,1)->value(‘name‘);
dump($res);

}
【获取单个字段一条】
public function index()
{
$res = App::where(‘id‘,1)->value(‘name‘);
dump($res);

}

【获取单个字段多条】
public function index()
{
$res = App::column(‘name‘,‘key‘);
dump($res);

}

使用模型添加数据

【create方法】
public function index()
{
// $res = App::create([
// ‘name‘=>‘三星‘,
// ‘is_encryption‘=>1,
// ‘key‘=>‘sx‘,
// ‘status‘=>1
// ]);
//当添加数据库没有的字段时,会报错,且不会添加,加第二个参数,true
// $res = App::create([
// ‘name‘=>‘苹果mac‘,
// ‘is_encryption‘=>1,
// ‘key‘=>‘mac‘,
// ‘status‘=>1,
// ‘momo‘=>‘123‘
// ],true);

//仅允许添加name和status两个字段
$res = App::create([
‘name‘=>‘macbook‘,
‘is_encryption‘=>1,
‘key‘=>‘book‘,
‘status‘=>1,
‘momo‘=>‘123‘
],[‘name‘,‘status‘]);
dump($res);
}

【save方法】

public function index()
{
// $appModel = new App;
// $res = $appModel->save([
// ‘name‘=>‘手机‘,
// ‘is_encryption‘=>1,
// ‘key‘=>‘sj‘,
// ‘status‘=>1,
// ‘demo‘=>111
// ]);
//过滤非数据库字段
// $appModel = new App;
// $res = $appModel->allowField(true)->save([
// ‘name‘=>‘手机‘,
// ‘is_encryption‘=>1,
// ‘key‘=>‘sj‘,
// ‘status‘=>1,
// ‘demo‘=>1111
// ]);

$appModel = new App;
$res = $appModel->allowField([‘name‘,‘is_encryption‘])->save([
‘name‘=>‘手机‘,
‘is_encryption‘=>1,
‘key‘=>‘sj‘,
‘status‘=>1,
‘demo‘=>1111
]);
dump($res);
}

【添加多条数据】
public function index()
{
$appModel = new App;
$res = $appModel->saveAll([
[‘name‘=>‘book‘],
[‘name‘=>‘cat‘]
]);
dump($res);
}

使用模型更新数据

【update方法更新】
public function index()
{
// $res = App::update([
// ‘id‘=>8,
// ‘is_encryption‘=>1
// ]);

//没有条件的话,update()加第二个参数
// $res = App::update([
// ‘is_encryption‘=>1,
// ],[‘id‘=>12]);

//还支持闭包函数
$res = App::update([
‘is_encryption‘=>1
],function($quest){
$quest->where("id",‘=‘,13);
});
dump($res);
}

【where条件更新】【推荐】
public function index()
{
// $res = App::where(‘id‘,5)->update([
// ‘key‘=>‘hw‘
// ]);
// dump($res);
}

【save方法更新】【推荐】
public function index()
{

// $appModel = new App;
// $res = $appModel->save([
// ‘key‘ => ‘mb‘,
// ],[‘id‘=>8]);

$appModel = new App;
$res = $appModel->save([
‘key‘ => ‘sj11‘,
],function($quest){
$quest->where(‘id‘,11);
});
dump($res);
}

【saveAll更新】
public function index()
{
$appModel = new App;

$res = $appModel->saveAll([
[‘id‘=>12,‘key‘=>‘bk‘],
[‘id‘=>13,‘key‘=>‘cat‘]
]);
dump($res);
}

使用模型删除数据

[destroy方法]
public function index()
{
// $res = App::destroy([‘id‘=>11]);
// dump($res);

// $res = App::destroy(function($quest){
// $quest->where(‘id‘,13);
// });

dump($res);
}

[delete方法]
public function index()
{
// $appModel = App::get(12);
// $res = $appModel->delete();

$res = App::where(‘id‘,‘>‘,7)->delete();
dump($res);
}

时间: 2024-08-01 21:03:59

ThinkPHP的Model操作的相关文章

thinkphp对数据库操作有哪些内置函数

原文:thinkphp对数据库操作有哪些内置函数 getModelName() 获取当前Model的名称 getTableName() 获取当前Model的数据表名称 switchModel(type,vars=array()) 动态切换模型 table() 设置当前操作的数据表 field() 设置要查询的数据字段 where() 设置查询或者操作条件 data(data) 设置数据对象 order(order) 设置排序 limit(limit) 查询限制 page(page) 查询分页 j

Python之路-(Django(csrf,中间件,缓存,信号,Model操作,Form操作))

csrf 中间件 缓存 信号 Model操作 Form操作 csrf: 用 django 有多久,我跟 csrf 这个概念打交道就有久了. 每次初始化一个项目时都能看到 django.middleware.csrf.CsrfViewMiddleware 这个中间件 每次在模板里写 form 时都知道要加一个 {% csrf_token %} tag 每次发 ajax POST 请求,都需要加一个 X_CSRFTOKEN 的 header 什么是 CSRF CSRF, Cross Site Req

Django—Model操作

Django 模型是与数据库相关的,与数据库相关的代码一般写在 models.py 中,Django 支持 Sqlite3.MySQL.PostgreSQL 等数据库,只需要在 settings.py 中配置即可,不用更改 models.py 中的代码,丰富的 API 极大的方便了使用. 一.创建表 1. 基本结构 1 from django.db import models 2 3 class UserInfo(models.Model): 4 #若不指定主键,则会自动创建int类型的id列为

Python之路【第二十二篇】:Django之Model操作

Django之Model操作 一.字段 AutoField(Field) - int自增列,必须填入参数 primary_key=True BigAutoField(AutoField) - bigint自增列,必须填入参数 primary_key=True 注:当model中如果没有自增列,则自动会创建一个列名为id的列 from django.db import models class UserInfo(models.Model): # 自动创建一个列名为id的且为自增的整数列 usern

ThinkPHP 模型(Model)命名规范

一个小问题搞了好久:如果数据库的表名中有下划线,那么在用thinkphp做自动完成时注意Model类的命名要变成驼峰法,文件名和类名都要变.( 另外注意:只有使用create方法创建数据时才能调用到自动验证和自动完成 ) ThinkPHP 模型(Model)命名规范 模型命名规范ThinkPHP 对数据库的表名和模型类的命名遵循一定的规范.首先数据库的表名和字段全部采用小写形式,模型类的命名规则是除去表前缀的数据表名称,并且首字母大写,然后加上模型类的后缀定义.一些例子如下:表名(不含前缀)模型

Laravel Eloquent ORM 数据model操作

Laravel ORM 数据model操作 注意:ORM关联操作最后一定要记得带get()方法!否则会获取不到数据,为null 如: posts表 comments表  id id  contents post_id comment post模型内定义一对多关系hasMany 1 //定义关联关系 2 public function comments(){ 3 return $this->hasMany(Comments::class,'post_id','id'); 4 } 1 //容器内调用

thinkphp的model的where条件的两种形式

thinkphp的model的where查询时有两种形式. $model->field('id')->where('customer_num is null or customer_num=""')->select(); $map['pid']=$ss; $map['type']=$s1; $list=$Region->field('id')->where($map)->select();

Django Web开发之model操作

model操作 model常用操作对应的就是数据库中的增删改查 参照文档https://docs.djangoproject.com/zh-hans/2.2/topics/db/models/ 示例文件 from django.db import models class Students(models.Model): """学生表""" username = models.CharField(max_length=64, null=False,

ThinkPHP的CURD操作

CURD:create(创建).update(更新).read(读取).delete(删除) 1.数据对象创建(create):  a.TP提供了create方法迅速的创建数据对象,可一自动的根据表单数据创建对象,支持数组,对象,甚至可以把一个数据对象创建给一个新的数据对象,如:    $User=M('User');    $User->name='ThinkPHP';    $User->email='[email protected]';    $Member=M('Member');