Yii2的save()方法容易出错的地方
如果save()返回true, 但是数据没有保存成功,则应该是开启了事务且已经回滚
如果save()返回false, 则使用$model->errors查看错误原因
可以设置$model的场景,对具体的场景进行验证; 没有指定场景的验证规则会在所有的场景通用
save()方法有两个参数,第一个参数为是否开启验证,第二个字段为验证的字段,但是会调用beforeBValidate()
源码如下所示:
public function save($runValidation = true, $attributeNames = null)
{
if ($this->getIsNewRecord()) {
return $this->insert($runValidation, $attributeNames);
} else {
return $this->update($runValidation, $attributeNames) !== false;
}
}
public function update($runValidation = true, $attributeNames = null)
{
if ($runValidation && !$this->validate($attributeNames)) {
return false;
}
return $this->updateInternal($attributeNames);
}
public function validate($attributeNames = null, $clearErrors = true)
{
if ($clearErrors) {
$this->clearErrors();
}
if (!$this->beforeValidate()) {
return false;
}
$scenarios = $this->scenarios();
$scenario = $this->getScenario();
if (!isset($scenarios[$scenario])) {
throw new InvalidParamException("Unknown scenario: $scenario");
}
if ($attributeNames === null) {
$attributeNames = $this->activeAttributes();
}
foreach ($this->getActiveValidators() as $validator) {
$validator->validateAttributes($this, $attributeNames);
}
$this->afterValidate();
return !$this->hasErrors();
}
protected function updateInternal($attributes = null)
{
if (!$this->beforeSave(false)) {
return false;
}
$values = $this->getDirtyAttributes($attributes);
if (empty($values)) {
$this->afterSave(false, $values);
return 0;
}
$condition = $this->getOldPrimaryKey(true);
$lock = $this->optimisticLock();
if ($lock !== null) {
$values[$lock] = $this->$lock + 1;
$condition[$lock] = $this->$lock;
}
// We do not check the return value of updateAll() because it's possible
// that the UPDATE statement doesn't change anything and thus returns 0.
$rows = static::updateAll($values, $condition);
if ($lock !== null && !$rows) {
throw new StaleObjectException('The object being updated is outdated.');
}
if (isset($values[$lock])) {
$this->$lock = $values[$lock];
}
$changedAttributes = [];
foreach ($values as $name => $value) {
$changedAttributes[$name] = isset($this->_oldAttributes[$name]) ? $this->_oldAttributes[$name] : null;
$this->_oldAttributes[$name] = $value;
}
$this->afterSave(false, $changedAttributes);
return $rows;
}
Yii2的save()方法容易出错的地方的更多相关文章
- Yii2 执行Save()方法失败,却没有错误信息
一般用$model->errors 就能查看到更新失败的原因,但是这次却什么错误信息都没有,最后发现是因为在模型类中定义了一个方法 public function beforeSave($ins ...
- yii2 框架的 save() 方法 执行模式条件。
save() 方法会调用 insert() 和 update() 中的一个, 用哪个取决于当前 AR 对象是不是新对象(在函数内部,他会检查 yii\db\ActiveRecord::isNewRe ...
- 菜鸟学SSH(九)——Hibernate——Session之save()方法
Session的save()方法用来将一个临时对象转变为持久化对象,也就是将一个新的实体保存到数据库中.通过save()将持久化对象保存到数据库需要经过以下步骤: 1,系统根据指定的ID生成策略,为临 ...
- 使用Storyboard拖线容易出错的地方
使用Storyboard拖线容易出错的地方: 在Storyboard中,选中某个控件,按住ctrl键进行拖线,建立Outlet和Action后,不能手动再去修改自动生成的代码,然后再次进行连线,这样会 ...
- ThinkPHP 更新数据 save方法
ThinkPHP save() 方法 ThinkPHP 中使用 save() 方法来更新数据库,并且也支持连贯操作的使用. 例子: public function update(){ header(& ...
- Django 资源 与 知识 Django中自建脚本并使用Django环境 model中的save()方法说明 filter()用法
Django 资源 与 知识 Django中自建脚本并使用Django环境 model中的save()方法说明 filter()用法 2018/11/06 Chenxin 资料说明 Django基础入 ...
- Django model重写save方法及update踩坑记录
一个非常实用的小方法 试想一下,Django中如果我们想对保存进数据库的数据做校验,有哪些实现的方法? 我们可以在view中去处理,每当view接收请求,就对提交的数据做校验,校验不通过直接返回错误, ...
- MongoDB中insert方法、update方法、save方法简单对比
MongoDB中insert方法.update方法.save方法简单对比 1.update方法 该方法用于更新数据,是对文档中的数据进行更新,改变则更新,没改变则不变. 2.insert方法 该方法用 ...
- Django笔记-post与get方法相关出错记录
1.刚刚调试一个注册的程序,blog.views.register里用了return HttpResponse方法返回了一个注册页面 register.html,后者用了method = " ...
随机推荐
- iptables 端口转发规则
玩 vps 的经常要用到端口转发用以实现更快的速度.比如 ovh 机房的网络我这里访问非常慢,用远程桌面会吐血的类型.所以就会用其他的线路作为跳板,比如洛杉矶,香港之类的.再比如如果需要一个日本 ip ...
- 前端框架Vue.js——vue-i18n ,vue项目中如何实现国际化
本项目利用 VueI18n 组件进行国际化,使用之前,需要进行安装 $ npm install vue-i18n 一.框架引入步骤: 1. 先在 main.js 中引入 vue-i18n. // 国 ...
- P1563 玩具谜题(简单模拟)
就是一个简单模拟 #include<iostream> #include<string> using namespace std; ; int in[maxn], x[maxn ...
- 6-MVC结构简介
一.javeEE的项目结构层次:MVC1.Model:模型层(DAO+业务层) 2.View:视图层 jsp3.Control:控制层 servlet 二.分层的原则:1.层与层之间松耦合,层内保持高 ...
- from __future__ import包的作用
__future__是python2的概念,其实是为了使用python2时能够去调用一些在python3中实现的特性 1.absolute_import from __future__ import ...
- P2251 质量检测--洛谷luogu
传送门 题目描述 为了检测生产流水线上总共N件产品的质量,我们首先给每一件产品打一个分数A表示其品质,然后统计前M件产品中质量最差的产品的分值Q[m] = min{A1, A2, ... Am},以及 ...
- 断路器(Curcuit Breaker)模式
在分布式环境下,特别是微服务结构的分布式系统中, 一个软件系统调用另外一个远程系统是非常普遍的.这种远程调用的被调用方可能是另外一个进程,或者是跨网路的另外一台主机, 这种远程的调用和进程的内部调用最 ...
- WCF系列教程之消息交换模式之请求与答复模式(Request/Reply)
1.使用WCF请求与答复模式须知 (1).客户端调用WCF服务端需要等待服务端的返回,即使返回类型是void (2).相比Duplex来讲,这种模式强调的是客户端的被动接受,也就是说客户端接受到响应后 ...
- 2018湘潭邀请赛C题(主席树+二分)
题目地址:https://www.icpc.camp/contests/6CP5W4knRaIRgU 比赛的时候知道这题是用主席树+二分,可是当时没有学主席树,就连有模板都不敢套,因为代码实在是太长了 ...
- C. Nastya Is Transposing Matrices
链接 [https://codeforces.com/contest/1136/problem/C] 题意 给你两个规模一样的矩阵 问是否可以通过不断选取A矩阵的子"方正"转置得到 ...