关于thinkphp5 使用模型save()更新数据提示 method not exist:thinkdbQuery-」 报错解决方案

    技术2022-08-01  81

    模型的新增和更新方法都是save方法,系统有一套默认的规则来识别当前的数据需要更新还是新增。

    实例化模型后调用save方法表示新增;

    查询数据后调用save方法表示更新;

    save方法传入更新条件后表示更新;

    如果你的数据操作比较复杂,

    可以显式的指定当前调用save方法是新增操作还是更新操作。

    如果你想使用TP5里边的save() 方法进行更新数据,如果按照一般情况使用where()判断更新条件的话就会出现以下报错:

    类似以下代码:

    r e s = m o d e l ( ′ C e s h i ′ ) − > w h e r e ( [ ′ u i d ′ = > res = model('Ceshi')->where(['uid'=> res=model(Ceshi)>where([uid=>uid])->allowField(true)->isUpdate(true)->save([‘status’=>1]); 1 正确的解决方式: 如果使用模型更新操作的时候不要使用where()来进行判断,需要使用isUpdate() 来判断,第一个参数是true,第二个参数写更新的条件。

    r e s = m o d e l ( ′ C e s h i ′ ) − > a l l o w F i e l d ( t r u e ) − > i s U p d a t e ( t r u e , [ ′ u i d ′ = > res = model('Ceshi')->allowField(true)->isUpdate(true,['uid'=> res=model(Ceshi)>allowField(true)>isUpdate(true,[uid=>uid])->save([‘status’=>1]); 1 或者是在save()的第二个参数加上更新条件

    r e s = m o d e l ( ′ C e s h i ′ ) − > a l l o w F i e l d ( t r u e ) − > i s U p d a t e ( t r u e ) − > s a v e ( [ ′ s t a t u s ′ = > 1 ] , [ ′ u i d ′ = > res = model('Ceshi')->allowField(true)->isUpdate(true)->save(['status'=>1],['uid'=> res=model(Ceshi)>allowField(true)>isUpdate(true)>save([status=>1],[uid=>uid]); 1 或者你用的是id主键字段为更新条件的话可以直接放在一个数组里边:

    $res = model(‘Ceshi’)->allowField(true)->isUpdate(true)->save([‘status’=>1,‘id’=>1]); 1 OK结束。 ————————————————

    原文链接:https://blog.csdn.net/weixin_42905245/article/details/97526155

    Processed: 0.012, SQL: 9