orm添加构造函数自动保存列名
parent
2ada0ab74c
commit
a018e4a13f
|
@ -5,7 +5,6 @@ namespace App\Controllers;
|
||||||
use Core\Model;
|
use Core\Model;
|
||||||
use \Core\View;
|
use \Core\View;
|
||||||
use App\Models;
|
use App\Models;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Home controller
|
* Home controller
|
||||||
*
|
*
|
||||||
|
@ -34,4 +33,9 @@ class Home extends \Core\Controller
|
||||||
public function renderAction() {
|
public function renderAction() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
public function docsAction(){
|
||||||
|
$docs = new Models\Doc();
|
||||||
|
//$this->JsonResp(200,$docs,"OK");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -8,15 +8,19 @@ use PDO;
|
||||||
|
|
||||||
class Doc extends Model
|
class Doc extends Model
|
||||||
{
|
{
|
||||||
|
public $tableName = 'doc';
|
||||||
|
public function __construct() {
|
||||||
|
parent::__construct();
|
||||||
|
}
|
||||||
/**
|
/**
|
||||||
* Get all the users as an associative array
|
* Get all the users as an associative array
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public static function getAll()
|
public function getAll()
|
||||||
{
|
{
|
||||||
$db = static::getDB();
|
$db = static::getDB();
|
||||||
$stmt = $db->query('SELECT * FROM users');
|
$stmt = $db->query('SELECT * FROM doc');
|
||||||
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
return $stmt->fectchAll;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -11,7 +11,7 @@ use PDO;
|
||||||
*/
|
*/
|
||||||
class User extends \Core\Model
|
class User extends \Core\Model
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get all the users as an associative array
|
* Get all the users as an associative array
|
||||||
*
|
*
|
||||||
|
@ -20,6 +20,7 @@ class User extends \Core\Model
|
||||||
public static function getAll() {
|
public static function getAll() {
|
||||||
$db = static::getDB();
|
$db = static::getDB();
|
||||||
$stmt = $db->query('SELECT * FROM users');
|
$stmt = $db->query('SELECT * FROM users');
|
||||||
|
|
||||||
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||||
}
|
}
|
||||||
public static function Insert($user){
|
public static function Insert($user){
|
||||||
|
@ -44,4 +45,5 @@ class User extends \Core\Model
|
||||||
);
|
);
|
||||||
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -12,7 +12,28 @@ use App\Config;
|
||||||
*/
|
*/
|
||||||
abstract class Model
|
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
|
* Get the PDO database connection
|
||||||
*
|
*
|
||||||
|
@ -40,7 +61,6 @@ abstract class Model
|
||||||
// Throw an Exception when an error occurs
|
// Throw an Exception when an error occurs
|
||||||
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $db;
|
return $db;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -29,7 +29,6 @@ $router->add('', ['controller' => 'Home', 'action' => 'index']);
|
||||||
$router->add('{controller}/{action}');
|
$router->add('{controller}/{action}');
|
||||||
$router->AddPrefix(\App\Config::ROUTE_PREFIX);
|
$router->AddPrefix(\App\Config::ROUTE_PREFIX);
|
||||||
try{
|
try{
|
||||||
//echo $_SERVER['REQUEST_URI'];
|
|
||||||
$router->dispatch($_SERVER['REQUEST_URI']);
|
$router->dispatch($_SERVER['REQUEST_URI']);
|
||||||
}catch (Exception $exception){
|
}catch (Exception $exception){
|
||||||
echo $exception;
|
echo $exception;
|
||||||
|
|
Loading…
Reference in New Issue