如何寫一個屬于自己的數據庫封裝(4)

測試數據庫來源

其實應該第一期就交出的, 但現在提起也無礙
參考了安裝安裝示例數據庫sakila

情景描述

我有一個用于測試的數據庫(sakila), 里頭有一個表(actor), 現在我們將它和安裝類綁定就可以很輕松寫意地讀取數據了


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

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)

? 版權聲明
THE END
喜歡就支持一下吧
點贊13 分享