MVC原理与数据模型类的编写
1. 应用入口
<?phpnamespace core;//应用入口,例如admin // admin.php => admin// echo strstr(basename(__FILE__),'.',true);echo basename(__FILE__,'.php');
2. 数据库连接类
<?php//数据库操作类namespace core;require __DIR__ . '/./funcs.php';require __DIR__ . '/./Query.php';define('CONFIG', require __DIR__ . '/../config.php');use PDO;class Db{ protected static PDO $db; //1.连接数据库 protected static function connect(string $dsn, string $username, string $password) { self::$db = new PDO($dsn, $username, $password); } //2.静态委托 //$name:方法名 //$arguments:参数数组 public static function __callStatic($name, $arguments) { //1.连接数据库 $dsn = CONFIG['database']['type']; $dsn .= ':dbname='; $dsn .= CONFIG['database']['dbname']; $username = CONFIG['database']['username']; $password = CONFIG['database']['password']; static::connect($dsn, $username, $password); //2.实例化数据库操作类Query.php $query = new Query(static::$db); return call_user_func_array([$query, $name], $arguments); }}// $result = Db::table('users')->field('id,uname,email')->where('id>3')->order('id','desc')->limit(3)->page(1)->select();// $rowCount = Db::table('users')->insert(['uname' => 'mhr', 'email' => 'mhr@qq.com']);// $rowCount = Db::table('users')->where('id=3')->update(['uname' => 'mhr', 'email' => 'mhr@qq.com']);$rowCount = Db::table('users')->where('id=3')->delete();d($rowCount);
3.公共函数库类
<?phpnamespace core;function d(...$args){ foreach ($args as $item) { $result = var_export($item,true); printf('<pre>%s</pre>',$result); }}
4.数据库配置类
<?php//框架的默认设置return [ //1.应用配置 'app' => [ 'debug' => false, 'default_controller' => 'Index', 'default_action' => 'customer', ], //2.数据库配置 'database' => [ 'type' => 'mysql', 'host' => '127.0.0.1', 'port' => '3306', 'username' => 'root', 'dbname' => 'phpedu', 'password' => 'root', 'charset' => 'utf8' ],];
5.增删改查类Query.php
<?phpnamespace core;use PDO;//数据库查询类class Query{ protected PDO $db; protected string $table; protected string $field = '*'; protected string $limit = '1'; protected string $where = ''; protected string $order = ''; //查询规则数组 protected array $opts = []; //构造器:连接数据库 public function __construct(PDO $db) { $this->db = $db; } //设置数据表 public function table(string $table): self { $this->table = $table; //返回当前对象,为了链式调用 return $this; } //设置数据表字段 public function field(string $field = '*'): self { $this->field = $field; return $this; } //设置数据表查询数量 public function limit(string $num = '1'): self { $this->limit = $num; $this->opts['limit'] = " LIMIT $num"; return $this; } //设置数据表分页数量 public function page(string $num = '1'): self { $this->opts['offset'] = ' OFFSET ' . ($num - 1) * $this->limit; return $this; } //设置数据表查询条件 public function where(string $where = ''): self { $this->opts['where'] = ' WHERE ' . $where; return $this; } //设置数据表排序方法 public function order($field, string $order = 'DESC'): self { $this->opts['order'] = ' ORDER BY ' . $field . ' ' . $order; return $this; } //执行多条查询 public function select(): array { $sql = 'SELECT ' . $this->field . ' FROM ' . $this->table; $sql .= $this->opts['where'] ?? null; $sql .= $this->opts['order'] ?? null; $sql .= $this->opts['limit'] ?? null; $sql .= $this->opts['offset'] ?? null; $stmt = $this->db->prepare($sql); $stmt->execute(); //查看生成的SQL语句 $stmt->debugDumpParams(); return $stmt->fetchAll(PDO::FETCH_ASSOC); } //单条查询 public function find(): array { $sql = 'SELECT ' . $this->field . 'FROM ' . $this->table; $sql .= $this->opts['where'] ?? null; //查询并验证 //获取结果集 $stmt = $this->db->prepare($sql); $stmt->execute(); //查看生成的SQL语句 $stmt->debugDumpParams(); //清空查询条件 $this->opts = []; return $stmt->fetch(PDO::FETCH_ASSOC); } //新增数据 public function insert(array $data): int { //拼接传入的关联数据 $str = ''; foreach ($data as $k => $v) { $str .= $k . '= "' . $v . '",'; } //拼装时,要去掉最后一个逗号 $sql = 'INSERT ' . $this->table . ' SET ' . rtrim($str, ', '); //查看SQL并进行测试 echo $sql; $stmt = $this->db->prepare($sql); $stmt->execute(); //外面判断返回值,就知道是否插入成功 return $stmt->rowCount(); } //更新数据 public function update(array $data): int { //Update()方法必须使用where()条件,禁止无条件更新 //拼接传入的数组 $str = ''; foreach ($data as $k => $v) { $str .= $k . '= "' . $v . '",'; } $sql = 'UPDATE ' . $this->table . ' SET ' . rtrim($str, ','); $sql .= $this->opts['where'] ?? die('禁止无条件更新'); //查看SQL语句并进行测试 echo $sql; $stmt = $this->db->prepare($sql); $stmt->execute(); return $stmt->rowCount(); } //删除数据 public function delete(): int { $sql = 'DELETE FROM ' . $this->table; $sql .= $this->opts['where'] ?? die('禁止无条件删除'); //查看SQL语句并进行测试 echo $sql; $stmt = $this->db->prepare($sql); $stmt->execute(); return $stmt->rowCount(); }}
4.