protected function parseUrl() { $pattern = '/^\\/' . '(?<class>[^\\/?]+)?' . '\\/?' . '(?<func>[^\\/?]+)?' . '\\/?' . '(?<params>[^\\/?]+(\\/[^\\/?]+)*)?' . '/is'; if (preg_match($pattern, URI, $uri_infos) == false) { header('Location: /index/notfound'); exit; } $uri_infos['class'] = isset($uri_infos['class']) ? ucfirst(strtolower($uri_infos['class'])) . 'Controller' : 'IndexController'; $uri_infos['func'] = isset($uri_infos['func']) ? StringOpt::unlinetocamel($uri_infos['func']) . 'Action' : 'listAction'; $uri_infos['params'] = isset($uri_infos['params']) ? explode('/', $uri_infos['params']) : array(); return array($uri_infos['class'], $uri_infos['func'], $uri_infos['params']); }
<?php require_once __DIR__ . '/../app/register.php'; $options = getopt('t:'); if (!isset($options['t'])) { echo 'usage: php create_model.php' . ' -t table' . PHP_EOL; exit; } $table = $options['t']; $table_class = ucfirst(StringOpt::unlinetocamel($table) . 'Model'); $file = MODEL_PATH . '/' . $table_class . '.php'; if (file_exists($file)) { echo '文件已存在,是否替换 [y/N]'; $sure = fgets(STDIN); if (trim($sure[0]) != 'Y' && trim($sure[0]) != 'y') { exit; } } $rp = new Repository('db', true); $pdo = $rp->getInstance(); $sql = 'describe ' . $table; $rs = $pdo->query($sql); $model = '<?php' . PHP_EOL . 'class ' . $table_class . PHP_EOL . '{' . PHP_EOL; $rows = $rs->fetchAll(); foreach ($rows as $row) { $model .= "\t" . 'private $' . $row['Field'] . ';' . PHP_EOL; } $model .= PHP_EOL; $model .= "\t" . 'public function __construct($params = array())' . PHP_EOL . "\t{" . PHP_EOL . "\t\t" . 'foreach (get_object_vars($this) as $key=>$value)' . PHP_EOL . "\t\t" . '{' . PHP_EOL . "\t\t\t" . 'if ($key != $this->get_pri_key() && isset($params[$key]))' . PHP_EOL . "\t\t\t\t" . '$this->$key = $params[$key];' . PHP_EOL . "\t\t\t" . 'else if (empty($this->$key))' . PHP_EOL . "\t\t\t\t" . '$this->$key = "";' . PHP_EOL . "\t\t}" . PHP_EOL . "\t}" . PHP_EOL . PHP_EOL; $model .= "\t" . 'public function get_model_fields()' . PHP_EOL . "\t" . '{' . PHP_EOL . "\t\t" . 'return array_keys(get_object_vars($this));' . PHP_EOL . "\t" . '}' . PHP_EOL; foreach ($rows as $row) {
public static function findBy($params) { // {{{ self::dbConnect(); if (empty(self::$table)) { return '{"code":-1, "errmsg":"table empty"}'; } $query_params = array(); $sql = 'select * from ' . self::$table . ' where 1' . self::getParams($params, $query_params); $stmt = self::$pdo_instance->prepare($sql); $table_class = ucfirst(StringOpt::unlinetocamel(self::$table) . 'Model'); $stmt->execute($query_params); $ret = $stmt->fetchAll(PDO::FETCH_CLASS, $table_class); return $ret; }