/** * @param config = [ * 'dbType' => 'mysql', * 'localhost' => 'localhost', * 'user' => 'root', * 'password' => '', * ] */ private static function _createDbInstance($dbInfo) { echobr('开始创建数据库实例'); //var_dump($dbInfo); $className = '\\vendor\\db\\' . $dbInfo['dbType'] . '\\' . ucfirst($dbInfo['dbType']); return new $className($dbInfo); }
public function getTableInfo() { // $tableInfo = Pii::app()->tableInfoPool; echobr('tableName : ' . $this->tableName()); // $tableInfo->getTableInfo($this->tableName()); return Pii::app()->tableInfoPool->getTableInfo($this->tableName()); //return $tableInfo->getTableInfo($this->tableName); }
private function _createTableInfo($tableName) { echobr('表的缓冲池没有表信息,开始实例化表信息'); $tmp = Pii::app()->db->getTableInfo($tableName); $tableInfo = []; foreach ($tmp as $info) { $tableInfo[$info['Field']] = $info; } return $tableInfo; }
private function _connect($dbInfo) { echobr('开始建立数据库连接'); $dsn = $dbInfo['dbType'] . ":host=" . $dbInfo['host'] . ';dbname=' . $dbInfo['dbName']; echobr('dsn:' . $dsn); $options = [PDO::ATTR_PERSISTENT => true, PDO::FETCH_ASSOC => 1]; try { $this->_connection = new PDO($dsn, $dbInfo['user'], $dbInfo['dbPassword'], $options); $this->_setEncoding($dbInfo['encoding']); } catch (\PDOException $e) { throw new PiiException($e->getMessage(), $e->getCode()); } echobr('实例化数据库连接完成'); }
/** * 后期可能加入依赖注入 */ public function createObject($className) { $config = Pii::app()->config; $classPrefix = $config->getValue($className, 'core'); if ($classPrefix) { echobr($classPrefix . '\\' . $className); return call_user_func([$classPrefix . '\\' . $className, 'getInstance']); } else { $className = $config->getValue($className, 'alias'); if (!$className) { throw new PiiException('there is no exist config for this name'); } return call_user_func([$className, 'getInstance']); } }
function autoload($className) { $fileName = dirname(__DIR__) . '/' . str_replace('\\', '/', $className) . '.php'; echobr('当前要实例化的:' . $fileName); //echo $fileName; if (file_exists($fileName)) { include $fileName; //echo $className . '<br>'; //只要require文件就可以了我擦... //return new $className; } else { $fileName = getClassFile($className); if ($fileName) { include $fileName; } } }
public function insert(ActiveRecord $ar) { $dirtyData = $ar->attributions; //vardumpbr($dirtyData); $tableName = $ar->tableName(); $this->_sql = "INSERT INTO {$tableName} SET "; $count = count($dirtyData); echobr($count); $i = 0; foreach ($dirtyData as $key => $attribute) { $this->_sql .= $attribute['Field'] . '=\'' . $attribute['value'] . '\''; if ($i < $count - 1) { $this->_sql .= ','; } $i++; } return $this; //$this->_sql = "INSERT INTO " }
public function actionAction() { echo 'now,you are access the action in test controller<br>'; echo 'the params are:<br>'; foreach ($this->_params as $key => $value) { echo 'key:' . $key . ' value:' . $value . "<br>"; } try { throw new PiiException(); } catch (PiiException $e) { echo "捕获到异常" . get_class($e) . '<br>'; } $model = new Test(); $model->name = '在下的第一条存储记录'; //var_dump($model); var_dump($model); $status = $model->save(); if ($status) { echobr('存储成功'); } }
/** * Starts application by disassembling the URL request and loading the appropriate controller, * invoking the appropriate method and its parameters (if requested) * */ public function __construct() { // disassembles the inbound URL request into its controller, method, and parameter parts $this->getUrlRequest(); /** * Debugging */ // print_var(URL_WITH_INDEX_FILE); $debug = 0; if ($debug == 1) { echobr('Controller'); print_var($this->urlController); echobr('Method'); print_var($this->urlMethod); echobr('Basename'); var_dump(basename($_SERVER['PHP_SELF'])); } /** * Load default controller otherwise load the controller corresponding to the client URL request * */ if (!$this->urlController) { require APP_PATH . 'controllers/IndexController.php'; $webpage = new IndexController(); $webpage->index(); } elseif (file_exists(APP_PATH . 'controllers/' . $this->urlController . '.php')) { // load the controller corresponding to the client URL request require APP_PATH . 'controllers/' . $this->urlController . '.php'; // instantiate the corresponding controller as an object $this->urlController = new $this->urlController(); /** * Invoke the method requested in the url for previously instantiated object, otherwise invoke * default index() method in the controller * */ if (method_exists($this->urlController, $this->urlMethod)) { // debug // var_dump($this->urlController); // var_dump($this->urlMethod); /** * Pass the parameters to the method if they exist otherwise invoke the method without parameters * */ if (!empty($this->urlParameter)) { // invoke the method and pass the parameters $this->urlController->{$this->urlMethod}($this->urlParameter); //debug // echo('<br>' . 'Requested Parameters: ' . $this->urlParameter . '<br>'); } else { $this->urlController->{$this->urlMethod}(); } } else { /** * Invoke the default index() method for the corresponding controller if no method was requested * otherwise redirect client to error page for tampering with the URL request * */ if ($this->urlMethod == null) { // invoke default index() method $this->urlController->index(); } else { // redirect to error page // echo "REQUESTED METHOD DOES NOT EXIST"; } } } else { // echo "TEST: " . $this->urlController; // redirect to error page // echo "CATASTROPHIC ERROR"; } }
<?php /** * Created by PhpStorm. * User: bob * Date: 2015/9/16 * Time: 上午 08:59 */ include_once "config.php"; include_once "function.php"; $conn = get_pdo_connect($host, $user, $pass, $dbname); $stmt = "SELECT * FROM contact"; echobr('===PDO driver: class style ===='); $rs = $conn->query($stmt); while ($row = $rs->fetch(1)) { print "<ul>"; foreach ($row as $item) { echoli($item); } print "</ul>"; }