当前位置:Gxlcms > PHP教程 > PHP封装的数据库模型Model类完整示例【基于PDO】

PHP封装的数据库模型Model类完整示例【基于PDO】

时间:2021-07-01 10:21:17 帮助过:80人阅读

本文实例讲述了PHP封装的数据库模型Model类。分享给大家供大家参考,具体如下:

  1. <?php
  2. //引入配置文件
  3. include "../Config/config.php";
  4. class Model extends PDO
  5. {
  6. protected $tableName = "";//存储表名
  7. protected $sql = "";//存储最后执行的SQL语句
  8. protected $limit = "";//存储limit条件
  9. protected $order = "";//存储order排序条件
  10. protected $field = "*";//存储要查询的字段
  11. protected $where = "";//存储where条件
  12. protected $allFields = [];//存储当前表的所有字段
  13. /**
  14. * 构造方法 初始化
  15. * @param string $tableName 要操作的表名
  16. */
  17. public function __construct($tableName)
  18. {
  19. //连接数据库
  20. parent::__construct('mysql:host='.HOST.';dbname='.DB.';charset=utf8;port='.PORT,USER,PWD);
  21. //存储表名
  22. $this->tableName = PRE.$tableName;
  23. //获取当前数据表中有哪些字段
  24. $this->getFields();
  25. }
  26. /**
  27. * 获取当前表的所有字段
  28. * @return array 成功则返回一维数组字段
  29. */
  30. public function getFields()
  31. {
  32. //查看当前表结构
  33. $sql = "desc {$this->tableName}";
  34. $res = $this->query($sql);//返回pdo对象
  35. //var_dump($res);
  36. if ($res) {
  37. $arr = $res->fetchAll(2);
  38. //var_dump($arr);
  39. //从二维数组中取出指定下标的列
  40. $this->allFields = array_column($arr,"Field");
  41. return $this->allFields;
  42. } else {
  43. die("表名错误");
  44. }
  45. }
  46. /**
  47. * 添加操作
  48. * @param array $data 要添加的数组
  49. * @return int 返回受影响行数
  50. */
  51. public function add($data)
  52. {
  53. //判断是否是数组
  54. if (!is_array($data)) {
  55. return $this;
  56. }
  57. //判断是否全是非法字段
  58. if (empty($data)) {
  59. die("非法字段");
  60. }
  61. //过滤非法字段
  62. foreach($data as $k => $v){
  63. if (!in_array($k,$this->allFields)) {
  64. unset($data[$k]);
  65. }
  66. }
  67. //将数组中的键取出
  68. $keys = array_keys($data);
  69. //将数组中取出的键转为字符串拼接
  70. $key = implode(",",$keys);
  71. //将数组中的值转化为字符串拼接
  72. $value = implode("','",$data);
  73. //准备SQL语句
  74. $sql = "insert into {$this->tableName} ({$key}) values('{$value}')";
  75. $this->sql = $sql;
  76. //执行并发送SQL,返回受影响行数
  77. return (int)$this->exec($sql);
  78. }
  79. /**
  80. * 删除操作
  81. * @param string $id 要删除的id
  82. * @return int 返回受影响行数
  83. */
  84. public function delete($id="")
  85. {
  86. //判断id是否存在
  87. if (empty($id)) {
  88. $where = $this->where;
  89. }else{
  90. $where = "where id={$id}";
  91. }
  92. $sql = "delete from {$this->tableName} {$where}";
  93. $this->sql = $sql;
  94. //执行并发送SQL,返回受影响行数
  95. return (int)$this->exec($sql);
  96. }
  97. /**
  98. * 修改操作
  99. * @param array $data 要修改的数组
  100. * @return int 返回受影响行数
  101. */
  102. public function update($data)
  103. {
  104. //判断是否是数组
  105. if (!is_array($data)){
  106. return $this;
  107. }
  108. //判断是否全是非法字段
  109. if (empty($data)) {
  110. die('全是非法字段');
  111. }
  112. $str = "";
  113. //过滤非法字段
  114. foreach ($data as $k=>$v) {
  115. //字段为id时,判断id是否存在的
  116. if ($k == "id"){
  117. $this->where = "where id={$v}";
  118. unset($data[$k]);
  119. continue;
  120. }
  121. //若字段不为id,则过滤后再拼接成set字段
  122. if (in_array($k, $this->allFields)) {
  123. $str .= "{$k}='{$v}',";
  124. } else {
  125. unset($data[$k]);
  126. }
  127. }
  128. //判断是否传了条件
  129. if (empty($this->where)) {
  130. die('请传入修改条件');
  131. }
  132. //去除右边的,
  133. $str = rtrim($str, ',');
  134. $sql = "update {$this->tableName} set {$str} {$this->where}";
  135. //echo $sql;
  136. $this->sql = $sql;
  137. return (int)$this->exec($sql);
  138. }
  139. /**
  140. * 查询多条数据
  141. * @return array 成功返回二维数组,失败返回空数组
  142. */
  143. public function select()
  144. {
  145. $sql = "select {$this->field} from {$this->tableName} {$this->where} {$this->order} {$this->limit}";
  146. $this->sql = $sql;
  147. //执行SQL,结果集是一个对象
  148. $res = $this->query($sql);
  149. //判断是否查询成功,
  150. if ($res){
  151. //成功返回二维数组
  152. return $res->fetchAll(2);
  153. }
  154. //失败返回空数组
  155. return [];
  156. }
  157. /**
  158. * 查询一条数组
  159. * @param string $id 要查询的id
  160. * @return array 返回一条数据
  161. */
  162. public function find($id="")
  163. {
  164. //判断是否存在id
  165. if (empty($id)){
  166. $where = $this->where;
  167. }else{
  168. $where = "where id={$id}";
  169. }
  170. $sql = "select {$this->field} from {$this->tableName} {$where} {$this->order} limit 1";
  171. $this->sql = $sql;
  172. //执行sql,结果集为对象
  173. $res = $this->query($sql);
  174. //判断是否查询成功
  175. if ($res){
  176. //成功则返回一条数据(一维数组)
  177. $result = $res->fetchAll(2);
  178. return $result[0];
  179. }
  180. //失败返回空数组
  181. return [];
  182. }
  183. /**
  184. * 统计总数目
  185. * @return int 返回总数
  186. */
  187. public function count()
  188. {
  189. $sql = "select count(*) as total from {$this->tableName} {$this->where} limit 1";
  190. $this->sql = $sql;
  191. //执行SQL,结果集为对象
  192. $res = $this->query($sql);
  193. //处理结果集
  194. if ($res){
  195. $result = $res->fetchAll(2);
  196. //var_dump($result);
  197. return $result[0]["total"];
  198. }
  199. return 0;
  200. }
  201. /**
  202. * 设置要查询的字段信息
  203. * @param string $field 要查询的字段
  204. * @return object 返回自己,保证连贯操作
  205. */
  206. public function field($field)
  207. {
  208. //判断字段是否存在
  209. if (empty($filed)){
  210. return $this;
  211. }
  212. $this->field = $field;
  213. return $this;
  214. }
  215. /**
  216. * 获取最后执行的sql语句
  217. * @return string sql语句
  218. */
  219. public function _sql()
  220. {
  221. return $this->sql;
  222. }
  223. /**
  224. * where条件
  225. * @param string $where 要输入的where条件
  226. * @return object 返回自己,保证连贯操作
  227. */
  228. public function where($where)
  229. {
  230. $this->where = "where ".$where;
  231. return $this;
  232. }
  233. /**
  234. * order条件
  235. * @param string $order 要输入的order条件
  236. * @return object 返回自己,保证连贯操作
  237. */
  238. public function order($order)
  239. {
  240. $this->order = "order by ".$order;
  241. return $this;
  242. }
  243. /**
  244. * limit条件
  245. * @param string $limit 要输入的limit条件
  246. * @return object 返回自己,保证连贯操作
  247. */
  248. public function limit($limit)
  249. {
  250. $this->limit = "limit ".$limit;
  251. return $this;
  252. }
  253. }

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP基于pdo操作数据库技巧总结》、《php+mysqli数据库程序设计技巧总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

人气教程排行