tp5--模型

(9)学习tp5之模型 模型命名:使用去掉前缀的表名(大驼峰).php注意:使用模型中记得修改配置文件// 数据集返回类型 'resultset_type' => 'collection', //手册-》数据库-》数据集1、路由2、控制器中的代码(当前是UserController控制器) //往模型中【插入】数据 public function add(){ //二维插入多条,一... 阅读详情

thinkphp5.0-模型

新版的模型进行了重构,更加对象化操作,包括关联模型的重构,主要特性包括:

  • 完全对象式访问
  • 支持静态调用(查询)
  • 支持读取器/修改器
  • 时间戳字段自动写入
  • 对象/数组访问
  • JSON序列化
  • 模型事件触发
  • 命名范围
  • 类型自动转换
  • 数据验证和自动完成
  • 关联查询/操作
  • 关联预载入

请不要以3.2版本的模型思维来使用5.0的模型,充分理解和掌握新版模 型的用法对于熟练掌握TP5有很大的帮助。

定义
版本 调整功能
5.0.5 model 或者 Loader::model 方法支持传入完整的模型类名
模型定义
定义一个User模型类:

namespace	app\index\model;
use	think\Model;
class	User	extends	Model
{
}

默认主键为自动识别,如果需要指定,可以设置属性:

namespace	app\index\model;
use	think\Model;
class	User	extends	Model
{
				protected	$pk	=	'uid';
}

模型会自动对应数据表,模型类的命名规则是除去表前缀的数据表名称,采用驼峰法命名,
并且首字母大写,例如:
模型名 约定对应数据表(假设数据库的前缀定义是 think_)

User think_user
UserType think_user_type

如果你的规则和上面的系统约定不符合,那么需要设置Model类的数据表名称属性,以确保
能够找到对应的数据表。
设置数据表
如果你想指定数据表甚至数据库连接的话,可以使用:
如果你的规则和上面的系统约定不符合,那么需要设置Model类的数据表名称属性,以确保
能够找到对应的数据表。
设置数据表
如果你想指定数据表甚至数据库连接的话,可以使用:

//	静态调用
$user	=	User::get(1);
$user->name	=	'thinkphp';
$user->save();
//	实例化模型
$user	=	new	User;
$user->name=	'thinkphp';
$user->save();
//	使用	Loader	类实例化(单例)
$user	=	Loader::model('User');
//	或者使用助手函数`model`
$user	=	model('User');
$user->name=	'thinkphp';
$user->save();

模型初始化
模型同样支持初始化,与控制器的初始化不同的是,模型的初始化是重写 Model 的
initialize ,具体如下

namespace	app\index\model;
use	think\Model;
class	Index	extends	Model
{
				//自定义初始化
				protected	function	initialize()
				{
								//需要调用`Model`的`initialize`方法
								parent::initialize();
								//TODO:自定义的初始化
				}
}

同样也可以使用静态 init 方法,需要注意的是 init 只在第一次实例化的时候执行,并且
方法内需要注意静态调用的规范,具体如下:

namespace	app\index\model;
use	think\Model;
class	Index	extends	Model
{
				//自定义初始化
				protected	static	function	init()
				{
								//TODO:自定义的初始化
				}
}

新增
新增数据有多种方式。
添加一条数据
第一种是实例化模型对象后赋值并保存:

$user											=	new	User;
$user->name					=	'thinkphp';
$user->email				=	'thinkphp@qq.com';
$user->save();
也可以使用 data 方法批量赋值:
$user	=	new	User;
$user->data([
				'name'		=>		'thinkphp',
				'email'	=>		'thinkphp@qq.com'
]);
$user->save();

或者直接在实例化的时候传入数据

$user	=	new	User([
				'name'		=>		'thinkphp',
				'email'	=>		'thinkphp@qq.com'
]);
$user->save();

如果需要过滤非数据表字段的数据,可以使用:

$user	=	new	User($_POST);
//	过滤post数组中的非数据表字段数据
$user->allowField(true)->save();

如果你通过外部提交赋值给模型,并且希望指定某些字段写入,可以使用:

$user	=	new	User($_POST);
//	post数组中只有name和email字段会写入
$user->allowField(['nam
实例讲解TP5中关联模型 一、关联模型 在关系型数据库中,表之间有一对一、一对多、多对多的关系。在 TP5 中,实现了ORM (Object Relational Mapping) 的思想,通过在模型中建立模型间的关联,实现建立表与表之间的关联。 二、文章中用到的表结构 image 表,存储图片的位置信息 banner 推荐位表,存储推荐位的类型 banner_item 表,推荐位中的信息 阅读详情

相关推荐

从备份到部署:用Clonezilla为网吧/机房批量克隆系统镜像的实战流程

本文详细介绍了使用Clonezilla为网吧/机房批量克隆系统镜像的实战流程,从准备工作到制作黄金镜像,再到批量部署系统镜像的全过程。通过Clonezilla这一开源工具,用户能够高效完成多台计算机的系统部署,确保环境一致性,大幅提升工作效率。文章还提供了优化技巧和常见问题解决方案,帮助用户顺利完成克隆任务。

weixin_34072296的博客 363

Unity序列帧动画输出全攻略:Recorder+Timeline实战与透明通道处理

序列帧动画是游戏开发、影视动画和UI动效中的基础技术,指将连续动作分解为一系列静态图像并按序播放,形成动态视觉效果。其原理是通过时间轴精确控制每一帧的内容与时机,实现动画的逐帧呈现。在实时渲染领域,序列帧输出技术价值显著,它能够将动态内容高质量地转换为静态图像序列,便于跨平台复用、后期合成与资源分发。应用场景广泛,包括角色动画导出、技能特效制作、宣传片素材生成等。本文聚焦Unity引擎,深入解析如何利用Recorder与Timeline工具链,高效输出带Alpha透明通道的PNG序列帧,并重点攻克透明通道处

weixin_31556767的博客 333

从选卡到验证:CST仿真GPU加速全流程实战指南

本文详细介绍了CST仿真中GPU加速的全流程实战指南,从硬件选型到软件配置,再到性能验证和常见问题排查。通过对比专业卡与游戏卡的性能差异,提供硬件选型建议,并分享驱动安装、关键设置及CST软件配置的黄金法则,帮助用户显著提升仿真效率。

weixin_30650039的博客 396

VMware如何安装img镜像,VMware如何安装openwrt软路由(含相关工具镜像)

VMware如何安装img镜像,VMware如何安装openwrt软路由(含相关工具镜像)

XiaoqiangClub的博客 4017

学 Simulink—— 基于强化学习(DDPG)的电机无位置传感器控制仿真

Actor:输入状态s,输出θ̂_dot(即 ω̂),积分得 θ̂Critic:评估"估计角度与反电动势模型一致性"的 Q 值奖励函数不依赖真实 θ(无监督式自监督),只用iαβ预测残差。

amy_mhd的博客 22

结构光相移法-多频外差原理与实践

结构光相移法是一种常用的三维形貌测量技术,它通过对投射到目标物体上的结构光进行相位调制和解调,实现对物体表面的三维重建。其中,多频外差原理是结构光相移法的核心原理之一,通过使用不同频率的结构光来实现相位测量,并可以提高测量精度和抗干扰性能。结构光相移法的多频外差原理是一种常用的三维形貌测量技术,它通过使用多个不同频率的结构光进行相位调制和解调,实现对物体表面的三维重建。本文介绍了多频外差原理的原理和实践应用,并提供了相应的Python源代码,希望对读者理解和应用结构光相移法有所帮助。

CodeByte的博客 739

【100%通过率】2023华为od机试C卷【电脑病毒感染】C语言 实现

一个局域网内有很多台电脑,分别标注为 0 ~ N-1 的数字。相连接的电脑距离不一样,所以感染时间不一样,感染时间用t 表示。其中网络内一台电脑被病毒感染,求其感染网络内所有的电脑最少需要多长时间。如果最后有电脑不会感染,则返回-1.

MISAYAONE的博客 1044

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

上一篇: tp5--控制器
活無常
博客等级 码龄8年 0粉丝 12原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址