示例#1
0
 /**
  * 简单定制一个规则,当传入的实体被用于更新或删除过,为了保险起见,直接返回pdoMaster;
  * 如果该实体未被用于删除或更新,则在pdoSlave队列中切出一个PDO并返回.切出的PDO放至队列的末尾。
  */
 public function getPdo($entity = null, $master = false)
 {
     $cache = CacheManager::getInstance();
     $entiList = $cache->get("inserted_or_deleted_or_updated_list");
     $class = @get_class($entity);
     if ($master || is_array($entiList) && in_array($class, $entiList)) {
         return $this->pdoMaster;
     }
     sort($this->pdoSlave);
     $pdo = @array_shift($this->pdoSlave);
     array_push($this->pdoSlave, $pdo);
     return $pdo;
 }
 /**
  * 获取 MongoGridFS 对象;
  * @param null $entity
  * @param bool $masger 启用主从;
  * @return \MongoGridFS
  */
 public function getGridFS($entity = null, $masger = false)
 {
     $mongodb = $this->mongoConnect->selectDB($_SERVER['mongodb']['gridfs_db']);
     $cache = CacheManager::getInstance();
     $entiList = $cache->get("inserted_or_deleted_or_updated_list");
     $class = @get_class($entity);
     //启用从库读
     if ($master || is_array($entiList) && in_array($class, $entiList)) {
         $this->mongoConnect->setSlaveOkay(true);
     } else {
         $this->mongoConnect->setSlaveOkay(false);
     }
     $grid = $mongodb->getGridFS();
     return $grid;
 }
 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;
 }
 /**
  * dump 结果
  * @param bool $toObject 转换为实体;
  * @return array
  * @throws
  */
 public function _array($toObject = true)
 {
     if ($this->tableIns == null) {
         try {
             $mongoManager = \my\bq\mdbao\MongoManager::getInstance();
             $this->mongoDatabase = $mongoManager->getDatabase($this->tableEntity);
         } catch (MongoConnectionException $e) {
             throw $e;
         }
     }
     $parmas = $this->getParameters();
     $order = $this->getOrderQuery();
     $cursor = null;
     //fields;
     $fields = $this->getFileds();
     $fetchFields = null;
     if (!($fields && $fields[0] == "*")) {
         if (is_array($fields)) {
             foreach ($fields as $k => $field) {
                 unset($fields[$k]);
                 if (is_object($field)) {
                     $fields[$field->getPropertyName()] = true;
                 } else {
                     $fields[$field] = true;
                 }
             }
             $fetchFields = $fields;
         }
     }
     if (Configuration::$SHOW_MONGO_QUERY) {
         $_Lginfo = array('query' => $parmas);
         if ($order) {
             $_Lginfo['order'] = $order;
         }
         if ($this->fetchSize > 0) {
             $_Lginfo['skip'] = array($this->firstResult, $this->fetchSize);
         }
         if ($fetchFields != null) {
             $_Lginfo['fields'] = array($fetchFields);
         }
         Log::writeMsg(Log::NOTICE, "MongoQuery SEARCH :[" . $this->table . "]  " . var_export($_Lginfo, true));
     }
     if ($this->modifier != null) {
         $cursor = $this->mongoDatabase->command(array('findandmodify' => $this->table, 'query' => $parmas, 'update' => $this->modifier));
         if ($cursor['lastErrorObject']['err'] != null) {
             Log::writeMsg(Log::ERROR, "MongoERR:[" . $this->table . "]" . var_export($cursor['lastErrorObject'], true));
             throw new \Exception($cursor['lastErrorObject']['err'], $cursor['lastErrorObject']['code']);
         }
     } else {
         $this->tableIns = $this->mongoDatabase->selectCollection($this->table);
         $cursor = $this->tableIns->find($parmas);
     }
     //has order
     if (is_array($order) && count($order) > 0) {
         $cursor->sort($order);
     }
     //has limit
     if ($this->fetchSize > 0) {
         $cursor->skip($this->firstResult)->limit($this->fetchSize);
     }
     //fields;
     if ($fetchFields != null) {
         $cursor->fields($fetchFields);
     }
     $dataArray = array();
     foreach ($cursor as $cur) {
         if (!$cur['_id']) {
             continue;
         }
         $d = array();
         $d['_id'] = $cur['_id'];
         if ($fields && $fields[0] == "*") {
             $this->fileds = $this->columns;
         }
         foreach ($this->fileds as $filed) {
             if (is_object($filed)) {
                 $f = explode('.', $filed->getPropertyName());
                 if (is_array($f)) {
                     $v = $cur[$f[0]];
                     unset($f[0]);
                     foreach ($f as $i) {
                         $v = $v[$i];
                     }
                     $d[$filed->getAlias()] = $v;
                 }
             } else {
                 $d[$filed] = $cur[$filed];
             }
         }
         $dataArray[] = $d;
     }
     if ($toObject) {
         $cache = CacheManager::getInstance();
         $tableEntitys = $cache->get('tableEntity');
         if (!$tableEntitys) {
             $tableEntitys = array($this->tableEntity);
         }
         $transData = $this->translaters;
         if (is_array($transData) && $transData[0]) {
             foreach ($dataArray as $k => $data) {
                 foreach ($transData as $dataTranslater) {
                     $property = $dataTranslater->getProperty();
                     if (!isset($data[$property->getPropertyName()])) {
                         break;
                     }
                     $value = $data[$property->getPropertyName()];
                     if ($value) {
                         $data[$property->getAlias()] = $dataTranslater->translate($value);
                         $dataArray[$k] = $data;
                     }
                 }
             }
         }
         if ($tableEntitys) {
             foreach ($dataArray as $k => $data) {
                 //if(Relation::$hasRelation) //has relation
                 //to object
                 $dataArray[$k] = $this->dataToObject($tableEntitys, $data);
             }
         }
     }
     Relation::$hasRelation = false;
     CacheManager::getInstance()->clean();
     return $dataArray;
 }
 public function _array($toObject = true)
 {
     $pdoManager = PdoManager::getInstance();
     $pdo = $pdoManager->getPdo($this->tableEntity);
     $sql = $this->sql();
     $parmas = $this->getParameters();
     $dataArray = $pdo->getRows($sql, $parmas);
     if ($toObject) {
         $cache = CacheManager::getInstance();
         $tableEntitys = $cache->get('tableEntity');
         if (!$tableEntitys) {
             $tableEntitys = array($this->tableEntity);
         }
         $transData = $this->getRootAndJoinsTranslater();
         if (is_array($transData) && $transData[0]) {
             foreach ($dataArray as $k => $data) {
                 foreach ($transData as $dataTranslater) {
                     if ($dataTranslater) {
                         $property = $dataTranslater->getProperty();
                         $dataKey = $this->getAlias() . "__" . $property->getPropertyName();
                         if (!array_key_exists($dataKey, $data)) {
                             continue;
                         }
                         $data[$this->getAlias() . "__" . $property->getAlias()] = $dataTranslater->translate($data[$dataKey]);
                         $dataArray[$k] = $data;
                     }
                 }
             }
         }
         if ($tableEntitys) {
             foreach ($dataArray as $k => $data) {
                 //if(Relation::$hasRelation) //has relation
                 //to object
                 $dataArray[$k] = $this->dataToObject($tableEntitys, $data);
             }
         }
     }
     Relation::$hasRelation = false;
     CacheManager::getInstance()->clean();
     return $dataArray;
 }
示例#6
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;
 }