我看到挺多代码都是
$order = new Order();
$res = $order->saveAll($lst);
获取
Order::create($lst)
并没有有类似这样的
$res=Order::create($lst)
if(!$res->id) 提示错误
有没必要增加这个判断呢 还是tp他会默认做了呢
我看到挺多代码都是
$order = new Order();
$res = $order->saveAll($lst);
获取
Order::create($lst)
并没有有类似这样的
$res=Order::create($lst)
if(!$res->id) 提示错误
有没必要增加这个判断呢 还是tp他会默认做了呢
这个问题,其实看一下源码就知道(TP版本5.0.22)
就你举例的这个create方法而言,主要代码是在
/thinkphp/library/think/Model.php
/thinkphp/library/think/db/Query.php
/thinkphp/library/think/db/Connection.php
三个文件中
最终的执行方法是Connection.php文件里的excute
方法,代码如下:
/**
* 执行语句
* @access public
* @param string $sql sql指令
* @param array $bind 参数绑定
* @param Query $query 查询对象
* @return int
* @throws PDOException
* @throws \Exception
*/
public function execute($sql, $bind = [], Query $query = null)
{
$this->initConnect(true);
if (!$this->linkID) {
return false;
}
// 记录SQL语句
$this->queryStr = $sql;
if ($bind) {
$this->bind = $bind;
}
Db::$executeTimes++;
try {
// 调试开始
$this->debug(true);
// 预处理
$this->PDOStatement = $this->linkID->prepare($sql);
// 是否为存储过程调用
$procedure = in_array(strtolower(substr(trim($sql), 0, 4)), ['call', 'exec']);
// 参数绑定
if ($procedure) {
$this->bindParam($bind);
} else {
$this->bindValue($bind);
}
// 执行语句
$this->PDOStatement->execute();
// 调试结束
$this->debug(false, '', true);
if ($query && !empty($this->config['deploy']) && !empty($this->config['read_master'])) {
$query->readMaster();
}
$this->numRows = $this->PDOStatement->rowCount();
return $this->numRows;
} catch (\PDOException $e) {
if ($this->isBreak($e)) {
return $this->close()->execute($sql, $bind, $query);
}
throw new PDOException($e, $this->config, $this->getLastsql());
} catch (\Throwable $e) {
if ($this->isBreak($e)) {
return $this->close()->execute($sql, $bind, $query);
}
throw $e;
} catch (\Exception $e) {
if ($this->isBreak($e)) {
return $this->close()->execute($sql, $bind, $query);
}
throw $e;
}
}
可以看到在插入操作数据库代码的外面加了一层try catch
的异常捕获,并且抛出了异常
由此,可以得到,你上面说的这种处理
$res=Order::create($lst)
if(!$res->id) 提示错误
其实也可以,但是非必要。加了是更严谨
一旦数据库操作报错,TP5源码中会直接抛出异常出来,你可以在模型操作外面加一层try catch捕获异常即可:
try {
Order::create($lst);
} catch (\PDOException $e) {
// TODO 这里处理执行失败之后的操作
} catch (\Exception $e) {
// TODO 这里处理执行失败之后的操作
}
答案是有,可以看/thinkphp/library/think/db/Query.php
文件的insert
方法,代码片段:
// 执行操作 (这里的execute的最终调用方法就是Connection.php的execute方法)
$result = 0 === $sql ? 0 : $this->execute($sql, $bind, $this);
if ($result) {
$sequence = $sequence ?: (isset($options['sequence']) ? $options['sequence'] : null);
$lastInsId = $this->getLastInsID($sequence);
if ($lastInsId) {
$pk = $this->getPk($options);
if (is_string($pk)) {
$data[$pk] = $lastInsId;
}
}
$options['data'] = $data;
$this->trigger('after_insert', $options);
if ($getLastInsID) {
return $lastInsId;
}
}
可以看到它也有处理,通过getLastInsID()
方法获取最后一次的插入id,会把id放在你上面$res = Order::create($lst)
中的$res变量中,所以你在后面加一步!$res->id
的判断也是可以的
你应该有答案了吧~
一般验证器验证通过了,新增这里是不会报错的,所以你看到的大部分都是没有在这里做判断。
再者,这里判断的意义其实不大,万一真的出现错误了,除了记录日志方便程序员回头排查,在用户体验上,没什么帮助。(只能显示类似:页面错误,请稍后再试,或系统异常,请联系管理员)
所以综合考量,一般也就懒得去做了,毕竟项目是需要赶时间的。
1 回答4k 阅读✓ 已解决
3 回答1.8k 阅读✓ 已解决
2 回答2.2k 阅读✓ 已解决
2 回答2.8k 阅读
1 回答1.4k 阅读✓ 已解决
2 回答2.2k 阅读
1 回答569 阅读✓ 已解决
错误还是要进行处理的,只是你们开发比较乐观,觉得不会出错,并没有捕获错误