zoukankan      html  css  js  c++  java
  • ThinkPHP Mongo驱动update方法支持upsert参数

    Mongo数据库update操作有一个相对于Mysql的关键特性,它可以使用upsert模式,当更新的数据不存在时,直接插入,但是ThinkPHP的Mongo驱动居然不支持这一特性,没办法,自力更生了。

    ThinkPHP的driver层,见由于支持多种DB,又使用了继承,使用得类层次结构较深,负责Mongo驱动的是DbMongo.class.php,文件位于ThinkPHP\Extend\Driver\Db,修改Update方法的$options参数,如果update方法提供了upsert选项时,就以upsert模式更新MongoDB。

       1:  public function update($data,$options) {
       2:          if(isset($options['table'])) {
       3:              $this->switchCollection($options['table']);
       4:          }
       5:          $this->model  =   $options['model'];
       6:          N('db_write',1);
       7:          $query   = $this->parseWhere($options['where']);
       8:          $set  =  $this->parseSet($data);
       9:          $isUpsert = $options['upsert'] === true ? true : false;
      10:          if($this->debug) {
      11:              $this->queryStr   =  $this->_dbName.'.'.$this->_collectionName.'.update(';
      12:              $this->queryStr   .= $query?json_encode($query):'{}';
      13:              $this->queryStr   .=  ','.json_encode($set).')';
      14:          }
      15:          try{
      16:              // 记录开始执行时间
      17:              G('queryStartTime');
      18:              $result   = $this->_collection->update($query,$set,array("upsert" => $isUpsert, "multiple" => true));
      19:              $this->debug();
      20:              return $result;
      21:          } catch (MongoCursorException $e) {
      22:              throw_exception($e->getMessage());
      23:          }
      24:      }

    这样使用update方法

       1:  $criteria[FollowerModel::UID] = $uid; 
       2:  $this->follower_model->where($criteria)->save($data, array('upsert'=>true));
  • 相关阅读:
    NGINX之——配置HTTPS加密反向代理訪问–自签CA
    AVPlayer的使用,带缓冲
    优化数据页面(15)——表题应当准确精练
    FFmpeg基础库编程开发学习笔记——音频常见格式及字幕格式
    【版本号公布】Jeecg-P3 1.0 公布,J2EE微服务框架(插件开发)
    猫猫学iOS之UILabel设置圆角不成功所做调控更改
    linux strace-跟踪进程的系统调用或是信号产生情况,lstrace-跟踪己丑年调用库函数情况,进程跟踪调试命令
    text
    sql server 2008 开启1433端口,开启远程连接
    openStack kvm 虚拟机CPU颗粒化控制
  • 原文地址:https://www.cnblogs.com/BlankEye/p/2786382.html
Copyright © 2011-2022 走看看