【PHP】MVC原理与数据模型类的编写
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__ . /系统开发mhkj33../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系统开发mhkj33 __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, $passwo系统开发mhkj33rd); //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([una系统开发mhkj33me => 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($ite系统开发mhkj33m,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 => 系统开发mhkj333306, 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; protect系统开发mhkj33ed 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; //返回当前对象,为了链式调用 系统开发mhkj33 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”; retur系统开发mhkj33n $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; retu系统开发mhkj33rn $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; 系统开发mhkj33 $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(); ret系统开发mhkj33urn $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(); /系统开发mhkj33/查看生成的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系统开发mhkj33 . “,; } //拼装时,要去掉最后一个逗号 $sql = INSERT . $this->table . SET . rtrim($str, , ); //查看SQL并进行测试 echo $sql; $stmt = $this->db->prepare($sql); $stmt->execute(); //外面判断返回值,就知道是否插入成功 return $stmt->rowCount(); } //更新数据 public function系统开发mhkj33 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(禁止无条件更系统开发mhkj33新); //查看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(禁止无条件删除); //查系统开发mhkj33看SQL语句并进行测试 echo $sql; $stmt = $this->db->prepare($sql); $stmt->execute(); return $stmt->rowCount(); }}
4.
© 版权声明
1.如需技术支持联系QQ:1339833655
2. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
3. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
4. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
5. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
6. 如有链接无法下载、失效或广告,请联系管理员处理!
7. 本站资源售价只是摆设,本站源码仅提供给会员学习使用!
8. 如遇到加密压缩包,请使用360解压,如遇到无法解压的请联系管理员!
THE END
暂无评论内容