예제 #1
0
 /**
  * @static
  * @param $entity
  * @param null $where 可以为 restrictions 构建的适应于 MongoDb的标准 Criteria; 或者 mongodb query;
  * @return bool|string
  * @throws MongoConnectionException
  * @throws MongoCursorException;
  */
 public static function updateEntity($entity, $where = null, $upsert = false)
 {
     if ($entity) {
         $entityCfg = $entity->getConfig();
         $vars = array();
         foreach ($entityCfg['columns'] as $key) {
             $vars[$key] = $entity->{$key};
             //为空的数据不再次刷到库中;
             if ($vars[$key] === null) {
                 unset($vars[$key]);
             }
         }
         //如果指定where 则使用where条件,未指定则使用id; 更新不可以不指定条件;
         $parseWhere = array();
         if ($where != null) {
             if (is_object($where) && method_exists($where, 'toMongoParam')) {
                 $parseWhere = array_merge($parseWhere, $where->toMongoParam($entity->getCriteria()));
             } else {
                 if (is_array($where)) {
                     $parseWhere = array_merge($parseWhere, $where);
                 }
             }
         } else {
             $parseWhere = array("_id" => $entity->_id);
         }
         $mongoTable = null;
         $keyId = "";
         try {
             $mongoManager = MongoManager::getInstance();
             $mongoDatabase = $mongoManager->getDatabase($entity, true);
             $mongoTable = $mongoDatabase->selectCollection($entityCfg['name']);
         } catch (MongoConnectionException $e) {
             throw $e;
         }
         try {
             if (isset($vars['_id'])) {
                 //mongodb不支持更新_id key
                 unset($vars['_id']);
             }
             if (Configuration::$SHOW_MONGO_QUERY) {
                 Log::writeMsg(Log::NOTICE, var_export(array('$set' => $vars, 'criteria' => $parseWhere), true));
             }
             $mongoTable->update($parseWhere, array('$set' => $vars), array('upsert' => $upsert, 'multiple' => true, 'safe' => true, 'fsync' => true, 'timeout' => 20000));
         } catch (MongoCursorException $me) {
             throw $me;
         }
         $cache = CacheManager::getInstance();
         $entiList = $cache->get("inserted_or_deleted_or_updated_list");
         if (!is_array($entiList)) {
             $entiList = array();
         }
         array_push($entiList, @get_class($entity));
         $cache->set("inserted_or_deleted_or_updated_list", $entiList);
         $relationCfg = $entity->getRelationPro();
         foreach ($relationCfg as $var => $conf) {
             if (isset($conf['cascade'])) {
                 $cascade = explode(",", $conf['cascade']);
                 if (in_array("update", $cascade)) {
                     $relationObject = $entity->{$conf}['name'];
                     //单个实体
                     if (is_object($relationObject)) {
                         $relationObject->{$conf}['column'] = $vars[$conf['key']];
                         self::updateEntity($relationObject);
                     }
                     //实体数组
                     if (is_array($relationObject)) {
                         foreach ($relationObject as $objectItem) {
                             if (is_object($objectItem)) {
                                 self::updateEntity($objectItem);
                             }
                         }
                     }
                 }
             }
         }
         return $keyId;
     }
     return false;
 }
 /**
  * 统计记录行数;
  * @param bool $countAll
  * @return array
  * @throws
  */
 public function _count($countAll = true)
 {
     if ($this->tableIns == null) {
         try {
             $mongoManager = \my\bq\mdbao\MongoManager::getInstance();
             $this->mongoDatabase = $mongoManager->getDatabase($this->tableEntity);
             $this->tableIns = $this->mongoDatabase->selectCollection($this->table);
         } catch (MongoConnectionException $e) {
             throw $e;
         }
     }
     $parmas = $this->getParameters();
     //$order = $this ->getOrderQuery();
     Log::writeMsg(Log::NOTICE, "MongoQuery:[" . $this->table . "]" . var_export($parmas, true));
     $cursor = $this->tableIns->find($parmas)->count(!$countAll);
     /*        if(is_array($order) && count($order)>0){
                 $cursor->sort($order);
             }*/
     if ($this->fetchSize > 0) {
         $cursor->skip($this->firstResult)->limit($this->fetchSize);
     }
     return $cursor;
 }
예제 #3
0
 public function execute(&$sth, $setFetchAssoc = true)
 {
     try {
         if ($setFetchAssoc) {
             $sth->setFetchMode(PDO::FETCH_ASSOC);
         }
         $this->debug($sth);
         $out = $sth->execute();
     } catch (\PDOException $e) {
         if (Configuration::$SHOW_CORE_EXCEPTION) {
             Log::writeMsg(Log::ERROR, $e->getMessage());
         }
         throw $e;
     }
     return $out;
 }
예제 #4
0
 /**
  * 执行指定SQL语句
  * @param string $sql
  * @param string $binds
  */
 public function execBySql($sql, $binds)
 {
     if (\my\bq\common\Configuration::$SHOW_SQL) {
         \my\bq\common\Log::writeMsg(\my\bq\common\Log::NOTICE, $sql);
     }
     try {
         $pdoManager = PdoManager::getInstance();
         $pdo = $pdoManager->getPdo();
         $sth = $pdo->prepare($sql);
         $pdo::bindValue($sth, $binds);
         $out = $pdo->execute($sth);
         $sth->closeCursor();
     } catch (\PDOException $e) {
         if (\my\bq\common\Configuration::$SHOW_CORE_EXCEPTION) {
             \my\bq\common\Log::writeMsg(\my\bq\common\Log::ERROR, $e->getMessage());
         }
         throw new DAOException(DAOException::DB_EXEC_EXCEPTION, DAOException::DB_EXEC_EXCEPTION_MESSAGE);
     }
     return $out;
 }
예제 #5
0
 public static function deleteEntity($entity, $where = null)
 {
     if ($entity) {
         $entityCfg = $entity->getConfig();
         $vars = array();
         foreach ($entityCfg['columns'] as $key) {
             $p = trim($key, "`");
             $vars[$key] = $entity->{$p};
         }
         //如果指定where 则使用where条件,未指定则使用id; 更新不可以不指定条件;
         $parseWhereStr = "";
         if ($where != null) {
             foreach ($where as $k => $v) {
                 if ($parseWhereStr != "") {
                     $parseWhereStr .= " and ";
                 }
                 if (is_string($v)) {
                     $v = "'" . $v . "'";
                 }
                 if (is_object($v)) {
                     $parseWhereStr .= $v->toSqlString();
                 } else {
                     $parseWhereStr .= "`" . $k . "` ='{$v}'";
                 }
             }
         } else {
             $id = explode(",", $entityCfg['id']);
             foreach ($id as $k) {
                 if ($parseWhereStr != "") {
                     $parseWhereStr .= " and ";
                 }
                 $parseWhereStr .= "`" . $k . "` =" . "'" . $entity->{$k} . "'";
             }
         }
         $sql = "delete from {$entityCfg['name']} where " . $parseWhereStr;
         if (Configuration::$SHOW_SQL) {
             Log::writeMsg(Log::NOTICE, $sql);
         }
         try {
             $pdoManager = PdoManager::getInstance();
             $pdo = $pdoManager->getPdo($entity, true);
             $bool = $pdo->exec($sql);
         } catch (Exception $e) {
             throw new DAOException(DAOException::DB_EXEC_EXCEPTION, DAOException::DB_EXEC_EXCEPTION_MESSAGE);
         }
         $cache = CacheManager::getInstance();
         $entiList = $cache->get("inserted_or_deleted_or_updated_list");
         if (!is_array($entiList)) {
             $entiList = array();
         }
         array_push($entiList, @get_class($entity));
         $cache->set("inserted_or_deleted_or_updated_list", $entiList);
         $relationCfg = $entity->getRelationPro();
         foreach ($relationCfg as $var => $conf) {
             if (isset($conf['cascade'])) {
                 $cascade = explode(",", $conf['cascade']);
                 if (in_array("delete", $cascade)) {
                     $relationObject = $entity->{$conf}['name'];
                     if (is_object($relationObject)) {
                         $relationObject = array($relationObject);
                     }
                     if (is_array($relationObject)) {
                         foreach ($relationObject as $object) {
                             try {
                                 $objConf = $object->getConfig();
                                 $objConf['id'] = $conf['column'];
                                 $object->setConfig($objConf);
                                 $object->{$objConf}['id'] = $entity->{$conf}['key'];
                                 self::deleteEntity($object);
                             } catch (Exception $e) {
                                 throw $e;
                             }
                         }
                     } else {
                         $columnVal = $entity->{$conf}['key'];
                         if (!$columnVal) {
                             $columnVal = $where[$conf['key']];
                         }
                         if ($columnVal) {
                             $classIns = new $conf['class']();
                             self::deleteEntity($classIns, array($conf['column'] => $columnVal));
                         }
                     }
                 }
             }
         }
         return $bool;
     }
     return false;
 }