/** * 执行指定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; }
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; }
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; }