如何寫一個屬於自己的資料庫封裝(4)

PHPz
發布: 2017-04-04 14:26:42
原創
1728 人瀏覽過


測試資料庫來源

其實應該第一期就交出的, 但現在提起也無礙
參考了安裝#mysql範例資料庫sakila

情境描述

我有一個用於測試的資料庫(sakila), 裡頭有一個表(actor), 現在我們將它和Model類別綁定就可以很輕鬆寫意地讀取資料了


首先, 新建一個類別, 類別名稱隨意, 但建議和表名一致

Actor .php

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

    // 设置Actor表的主键
    protected $identity = &#39;actor_id&#39;;

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

}
登入後複製

發現了嗎?是不是非常的簡潔? 除開所有註解你甚至只需一行就設定完了


舉個例子

  • 無需條件我就只是想讀取表裡的所有資料

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

$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)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!