From a018e4a13fe4d675064a323e7bb50d87a2138c39 Mon Sep 17 00:00:00 2001 From: a74589669 <290198252@qq.com> Date: Sat, 17 Aug 2019 02:28:53 +0800 Subject: [PATCH] =?UTF-8?q?orm=E6=B7=BB=E5=8A=A0=E6=9E=84=E9=80=A0?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E8=87=AA=E5=8A=A8=E4=BF=9D=E5=AD=98=E5=88=97?= =?UTF-8?q?=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- App/Controllers/Home.php | 6 +++++- App/Models/Doc.php | 10 +++++++--- App/Models/User.php | 4 +++- Core/Model.php | 22 +++++++++++++++++++++- public/index.php | 1 - 5 files changed, 36 insertions(+), 7 deletions(-) diff --git a/App/Controllers/Home.php b/App/Controllers/Home.php index b02dbda..c670073 100644 --- a/App/Controllers/Home.php +++ b/App/Controllers/Home.php @@ -5,7 +5,6 @@ namespace App\Controllers; use Core\Model; use \Core\View; use App\Models; - /** * Home controller * @@ -34,4 +33,9 @@ class Home extends \Core\Controller public function renderAction() { } + public function docsAction(){ + $docs = new Models\Doc(); + //$this->JsonResp(200,$docs,"OK"); + } } + diff --git a/App/Models/Doc.php b/App/Models/Doc.php index 4cb76e7..5be490d 100644 --- a/App/Models/Doc.php +++ b/App/Models/Doc.php @@ -8,15 +8,19 @@ use PDO; class Doc extends Model { + public $tableName = 'doc'; + public function __construct() { + parent::__construct(); + } /** * Get all the users as an associative array * * @return array */ - public static function getAll() + public function getAll() { $db = static::getDB(); - $stmt = $db->query('SELECT * FROM users'); - return $stmt->fetchAll(PDO::FETCH_ASSOC); + $stmt = $db->query('SELECT * FROM doc'); + return $stmt->fectchAll; } } \ No newline at end of file diff --git a/App/Models/User.php b/App/Models/User.php index 25f57c4..7d403fd 100644 --- a/App/Models/User.php +++ b/App/Models/User.php @@ -11,7 +11,7 @@ use PDO; */ class User extends \Core\Model { - + /** * Get all the users as an associative array * @@ -20,6 +20,7 @@ class User extends \Core\Model public static function getAll() { $db = static::getDB(); $stmt = $db->query('SELECT * FROM users'); + return $stmt->fetchAll(PDO::FETCH_ASSOC); } public static function Insert($user){ @@ -44,4 +45,5 @@ class User extends \Core\Model ); return $stmt->fetchAll(PDO::FETCH_ASSOC); } + } diff --git a/Core/Model.php b/Core/Model.php index 4650c28..5d4f094 100644 --- a/Core/Model.php +++ b/Core/Model.php @@ -12,7 +12,28 @@ use App\Config; */ abstract class Model { + public $tableName; + public $columns = array(); + //获得一个表的所有列名 + function __construct(){ + $this->getColumns($this->tableName); + } + public function getColumns($tableName) { + $db = static::getDB(); + $sql = "show columns from ".$tableName; + $stmt = $db->query($sql); + $res = $stmt->fetchAll(); + if(static::getDB()!=null){ + try{ + foreach ($res as $key => $value){ + array_push($res,$value[0]); + } + }catch (\PDOException $e){ + echo $e->getMessage(); + } + } + } /** * Get the PDO database connection * @@ -40,7 +61,6 @@ abstract class Model // Throw an Exception when an error occurs $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } - return $db; } } diff --git a/public/index.php b/public/index.php index fcf90d6..aff510f 100644 --- a/public/index.php +++ b/public/index.php @@ -29,7 +29,6 @@ $router->add('', ['controller' => 'Home', 'action' => 'index']); $router->add('{controller}/{action}'); $router->AddPrefix(\App\Config::ROUTE_PREFIX); try{ - //echo $_SERVER['REQUEST_URI']; $router->dispatch($_SERVER['REQUEST_URI']); }catch (Exception $exception){ echo $exception;