问题描述
写代码肯定有失误的时候,当查询参数出错时,如何输出错误信息呢?以前用thinkphp5在报错误的时候,可以通过DB::select(false)直接返回错误脚本,但是现在不可以了,会抛出异常,这样如何处理呢?
解决办法
代码是CRMEB下的,使用Dao进行处理
<?php
namespace app\common\dao\store;
use app\common\dao\BaseDao;
use app\common\model\store\StoreCategory as model;
use think\Log;
class StoreCategoryDao extends BaseDao
{
private $log;
public function __construct(Log $log)
{
$this->log = $log;
}
public function getModel(): string
{
return model::class;
}
public function test(?int $merId,$field,$value,$except = null)
{
$query = $this->getModel()::getDB();
try {
// 故意让脚本出错
$lcc = ($query)->where(["xxxxxxxxx" => 171])->select(false);
} catch (\Throwable $th) {
$sql = ($query)->getLastSql();
$this->log->error("StoreCategoryDao.test:".$sql);
}
return $data;
}
}