easy-swoole / orm

31 stars 27 forks source link

使用orm查询构造器不报异常 #44

Closed lxyzsl closed 4 years ago

lxyzsl commented 4 years ago

为什么使用orm的查询构造器后者插入数据时,写错字段名或者表名或者模型里定义时间戳字段错误的情况下没有报异常?

getchu commented 4 years ago

你可以在create之后返回 数据表的Model对象, 然后在对象上 获取 操作异常的SQL

        $prospectiveCustomer = \App\Model\ProspectiveCustomer::create($info);
        $id = $prospectiveCustomer->save(true, true);//update( ) 返回 bool
        if(!$id){
            $this->writeJson(400, null, '保存失败, SQL: '.
               $prospectiveCustomer->lastQuery()->getLastQuery()  );
            return true;
        }
getchu commented 4 years ago
    try{
        $prospectiveCustomer = \App\Model\ProspectiveCustomer::create($info);
        $id = $prospectiveCustomer->save(true, true);
        if(!$id){
            $this->writeJson(400, null, '保存失败');
            return true;
        }
    }catch (\Throwable $throwable) {
        $this->writeJson(400, null, '保存失败, 操作太频繁');
        return true;
    }
xuanyanwow commented 4 years ago

orm底层不会抛出异常,如swoole官方支持,查询失败会在结果对象设置errno error,需要先行判断是否存在失败情况。

参照文档 http://www.easyswoole.com/Components/Orm/lastResult.html

如果习惯抛出异常,可以自行封装基类Model 继承es官方Model 重写get all等方法,内部判断error 存在error则抛出异常