• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase
您的位置:首页 > 数据库 >Mysql > 如何写一个属于自己的数据库封装(4)

如何写一个属于自己的数据库封装(4)

作者:匿名 字体:[增加 减小] 来源:互联网 时间:2018-12-05

匿名通过本文主要向大家介绍了数据库封装等相关知识,希望本文的分享对您有所帮助

测试数据库来源

其实应该第一期就交出的, 但现在提起也无碍
参考了安装mysql示例数据库sakila

情景描述

我有一个用于测试的数据库(sakila), 里头有一个表(actor), 现在我们将它和Model类绑定就可以很轻松写意地读取数据了


首先, 新建一个类, 类名随意, 但建议和表名一致

Actor.php

<?php
/**
* 数据库中的Actor表
* 继承Model的属性和函数
*/
class Actor extends Model {
    // 由于我们的数据库表名和当前的类名是一样的,可以直接省略这一步
    // protected $table = 'Actor';

    // 设置Actor表的主键
    protected $identity = 'actor_id';

    // 或者设置unique key
    // 如果unique key只有一个
    // protected $unique = 'actor_id';
    // 如果多个unique key
    // protected $unique = ['first_name', 'last_name'];

}

发现了吗?是不是非常的简洁? 除开所有注释你甚至只需一行就设置完毕了


举个例子

  • 无需条件我就只是想读取表里的所有数据

<?php
/**
 * 特别预告
 * 想必有人发现需要require的文件可能多了一些
 * 这可以用自动载入来解决
 * 敬请期待
 */
// 辅助函数我都写helper里了
require 'lib/helper.php';
require 'lib/Connector.php';
require 'lib/Builder.php';
require 'lib/Grammar.php';
require 'lib/Model.php';
require 'model/Actor.php';

$a = Actor::get();
dd($a);

返回结果

array (size=197)
  0 => 
    object(Actor)[207]
      public 'actor_id' => string '1' (length=1)
      public 'first_name' => string 'PENELOPE' (length=8)
      public 'last_name' => string 'GUINESS' (length=7)
      public 'last_update' => string '2006-02-15 04:34:33' (length=19)
  1 => 
    object(Actor)[211]
      public 'actor_id' => string '2' (length=1)
      public 'first_name' => string 'NICK' (length=4)
      public 'last_name' => string 'WAHLBERG' (length=8)
      public 'last_update' => string '2006-02-15 04:34:33' (length=19)
  2 => 
    object(Actor)[215]
      public 'actor_id' => string '3' (length=1)
      public 'first_name' => string 'ED' (length=2)
      public 'last_name' => string 'CHASE' (length=5)
      public 'last_update' => string '2006-02-15 04:34:33' (length=19)
  3 => 
    object(Actor)[219]
      public 'actor_id' => string '4' (length=1)
      public 'first_name' => string 'JENNIFER' (length=8)
      public 'last_name' => string 'DAVIS' (length=5)
      public 'last_update' => string '2006-02-15 04:34:33' (length=19)

// 更多数据......

可读性更高且适用于当前要求的写法

// 这回多一个小要求, 我只想看以下两个声明的字段
$a = Actor::all(['first_name', 'last_name']);

或者

$a = Actor::select('first_name', 'last_name')->get();

返回结果

array (size=197)
  0 => 
    object(Actor)[207]
      public 'first_name' => string 'ADAM' (length=4)
      public 'last_name' => string 'GRANT' (length=5)
  1 => 
    object(Actor)[211]
      public 'first_name' => string 'ADAM' (length=4)
      public 'last_name' => string 'HOPPER' (length=6)
  2 => 
    object(Actor)[215]
      public 'first_name' => string 'AL' (length=2)
      public 'last_name' => string 'GARLAND' (length=7)
  3 => 
    object(Actor)[219]
      public 'first_name' => string 'ALAN' (length=4)
      public 'last_name' => string 'DREYFUSS' (length=8)

......
  • 我想搜索first_name中包含L这个字母的数据, 但不要前5条数据, 取之后的10条数据, 想查看first_name和last_name这两个字段就足够了
    最具可读性的写法,
    再预告, 分页(paginate)的雏形就在这里

    $a = Actor::select('first_name', 'last_name')
      ->where('first_name', 'like', '%L%')
      ->skip(5) // 略过5条
      ->take(10) // 拿取10条
      ->get();

    返回结果

    array (size=10)
    0 => 
      object(Actor)[20]
        public 'first_name' => string 'ANGELA' (length=6)
        public 'last_name' => string 'HUDSON' (length=6)
    1 => 
      object(Actor)[24]
        public 'first_name' => string 'ANGELA' (length=6)
        public 'last_name' => string 'WITHERSPOON' (length=11)
    2 => 
      object(Actor)[28]
        public 'first_name' => string 'ANGELINA' (length=8)
        public 'last_name' => string 'ASTAIRE' (length=7)
    3 => 
      object(Actor)[32]
        public 'first_name' => string 'BELA' (length=4)
        public 'last_name' => string 'WALKEN' (length=6)
    4 => 
      object(Actor)[36]
        public 'first_name' => string 'CHARLIZE' (length=8)
        public 'last_name' => string 'DENCH' (length=5)
    5 => 
      object(Actor)[40]
        public 'first_name' => string 'DARYL' (length=5)
        public 'last_name' => string 'CRAWFORD' (length=8)
    6 => 
      object(Actor)[44]
        public 'first_name' => string 'DARYL' (length=5)
        public 'last_name' => string 'WAHLBERG' (length=8)
    7 => 
      object(Actor)[48]
        public 'first_name' => string 'ELLEN' (length=5)
        public 'last_name' => string 'PRESLEY' (length=7)
    8 => 
      object(Actor)[52]
        public 'first_name' => string 'ELVIS' (length=5)
        public 'last_name' => string 'MARX' (length=4)
    9 => 
      object(Actor)[56]
        public 'first_name' => string 'EMILY' (length=5)
        public 'last_name' => string 'DEE' (length=3)

    想要更多的例子?留言吧我会在评论区给出函数链


调试小技巧

有时候可能对SQL语句的不理解导致跳BUG了, 这时候你想看看原生的SQL语句, 可以在Connector.php中的read()函数处, 这样加上

// 读取数据
    public function read($sql, $bindings) {
        var_dump($sql); // 就是这一句
        var_dump($bindings); // 还可以确认条件值是否正确对应
        // 将sql语句放入预处理函数
        $statement = $this->connection->prepare($sql);
        // 将附带参数带入pdo实例
        $this->bindValues($statement, $bindings);
        // 执行
        $statement->execute();
        // 返回所有合法数据, 以Object对象为数据类型
        return $statement->fetchAll(PDO::FETCH_OBJ);
    }

以最后一个例子为说明, 会输出这么两行

string 'select first_name, last_name from Actor where Actor.first_name like ? order by 1 asc limit 10 offset 5' (length=102)

array (size=1)
  0 => string '%L%' (length=3)

以上就是如何写一个属于自己的数据库封装(4)的详细内容,更多请关注微课江湖其它相关文章!

分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

相关文章

  • 2018-12-05SQL优化基础 使用索引(一个小例子)
  • 2018-12-05sql语句优化之用EXISTS替代IN、用NOT EXISTS替代NOT IN的语句
  • 2018-12-05高访问量的评论系统数据库存储过程架构
  • 2017-05-11MySQL数据库备份与恢复方法
  • 2018-12-05PHP连接数据库,通过面向过程方法实现最基本的增删改查操作
  • 2018-12-05关于mysql进阶的10篇课程推荐
  • 2018-12-05主键和唯一索引的有什么区别
  • 2018-12-05MySQL优化-常用函数代码详解(图)
  • 2018-12-05sql2008 附加数据库时出现错误5123提示的解决方法
  • 2018-12-05如何从官网下载最新MySQL安装包?

文章分类

  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase

最近更新的内容

    • MySQL 的日常使用
    • MySQL 编码机制
    • 深入解析mysql中order by与group by的顺序问题
    • linux修改mysql数据库文件的路径
    • Mysql相关_MySQL
    • 最长用最基本的MSSQL数据库备份与还原
    • MySQL配置文件my.cnf中文详解附mysql性能优化方法分享
    • 关于MySQL数据迁移--data目录直接替换注意事项的详解
    • Sql Server 字符串聚合函数
    • [Cassandra] cqlsh command list

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有