/** * 返回集合 int 类型的自增ID; * @static * @param String $collection 集合名称 或者 实现 EntityTemplate 的 实体对象; * @return int; * @throws MongoConnectionException; */ public static function nextIncId($collection) { if (self::$mongoDatabase == null) { $mongMgr = null; try { $mongMgr = MongoManager::getInstance(); self::$mongoDatabase = $mongMgr->getDatabase(null, true); } catch (MongoConnectionException $e) { throw $e; } } $tableName = null; if (is_string($collection)) { $tableName = $collection; } if (is_object($collection)) { $config = $collection->getConfig(); $tableName = $config['name']; } if ($tableName == null) { throw new \Exception("无法正常得到指定集合名称"); } $next_id = self::genId($tableName); if (!$next_id) { $next_id = self::genId($tableName); //确保第一次生产ID从 1 开始; } return $next_id; }
public static function deleteEntity($entity, $where = null) { if ($entity) { $entityCfg = $entity->getConfig(); $vars = array(); foreach ($entityCfg['columns'] as $key) { $vars[$key] = $entity->{$key}; } //如果指定where 则使用where条件,未指定则使用id; 更新不可以不指定条件; $parseWhere = array(); if ($where != null) { if (is_object($where) && method_exists($where, 'toMongoParam')) { array_push($parseWhere, $where->toMongoParam($entity->getCriteria())); } else { if (is_array($where)) { $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 { $rs = $mongoTable->remove($parseWhere, array('safe' => true, 'fsync' => false, 'timeout' => 20000)); $keyId = $rs['n']; if ($rs['err'] != null) { throw new Exception($rs['err'], \my\bq\dao\DAOException::DB_EXEC_EXCEPTION); } } 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::deleteEntity($relationObject); } //实体数组 if (is_array($relationObject)) { foreach ($relationObject as $objectItem) { if (is_object($objectItem)) { self::deleteEntity($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; }