上一期 如何寫一個屬于自己的數(shù)據(jù)庫封裝(3) - 查詢 - 入門篇
下一期 如何寫一個屬于自己的數(shù)據(jù)庫封裝(5) - 查詢 - JOIN篇
測試數(shù)據(jù)庫來源
其實應(yīng)該第一期就交出的, 但現(xiàn)在提起也無礙
參考了安裝mysql示例數(shù)據(jù)庫sakila
情景描述
我有一個用于測試的數(shù)據(jù)庫(sakila), 里頭有一個表(actor), 現(xiàn)在我們將它和Model類綁定就可以很輕松寫意地讀取數(shù)據(jù)了
首先, 新建一個類, 類名隨意, 但建議和表名一致
Actor.php
<?php
/**
* 數(shù)據(jù)庫中的 Actor 表
* 繼承 Model 的屬性和函數(shù)
*/
class Actor extends Model {
// 由于我們的數(shù)據(jù)庫表名和當(dāng)前的類名是一樣的,可以直接省略這一步
// protected $table = 'Actor';
// 設(shè)置 Actor 表的主鍵
protected $identity = 'actor_id';
// 或者設(shè)置 unique key
// 如果 unique key 只有一個
// protected $unique = 'actor_id';
// 如果多個 unique key
// protected $unique = ['first_name', 'last_name'];
}
發(fā)現(xiàn)了嗎?是不是非常的簡潔? 除開所有注釋你甚至只需一行就設(shè)置完畢了
例子
特別預(yù)告
想必有人發(fā)現(xiàn)需要 require 的文件可能多了一些
這可以用自動載入來解決
敬請期待
先載入需求文件
<?php
// 輔助函數(shù)我都寫 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';
- 讀取表里所有的數(shù)據(jù)
Actor::get();
或者
// 更可讀的寫法, 列出所有(all)的 Actor
Actor::all();
返回結(jié)果
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)
......
- 只查詢指定的字段 ('first_name', 'last_name')
Actor::get(['first_name', 'last_name']);
或者
Actor::select('first_name', 'last_name')->get();
或者
Actor::select(['first_name', 'last_name'])->get();
返回結(jié)果
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)
......
- 列出所有數(shù)據(jù), 以 first_name 順序排列, 但不要前5條數(shù)據(jù), 取之后的10條數(shù)據(jù), 查詢first_name和last_name兩個字段
最具可讀性的寫法,
再預(yù)告, 分頁(paginate)的雛形就在這里
Actor::select('first_name', 'last_name')
->skip(5) // 略過5條
->take(10) // 拿取10條
->orderBy('first_name')
->get();
返回結(jié)果
array (size=10)
0 =>
object(Actor)[20]
public 'first_name' => string 'ALBERT' (length=6)
public 'last_name' => string 'NOLTE' (length=5)
1 =>
object(Actor)[24]
public 'first_name' => string 'ALEC' (length=4)
public 'last_name' => string 'WAYNE' (length=5)
2 =>
object(Actor)[28]
public 'first_name' => string 'ANGELA' (length=6)
public 'last_name' => string 'HUDSON' (length=6)
......
想要更多的例子?留言吧我會在評論區(qū)給出函數(shù)鏈
調(diào)試小技巧
有時候可能對 SQL 語句的不理解導(dǎo)致跳 BUG 了, 這時候你想看看原生的SQL語句, 可以在 Connector.php 中的 read() 函數(shù)處, 這樣加上
// 讀取數(shù)據(jù)
public function read($sql, $bindings) {
var_dump($sql); // 就是這一句
var_dump($bindings); // 還可以確認(rèn)條件值是否正確對應(yīng)
// 將sql語句放入預(yù)處理函數(shù)
$statement = $this->connection->prepare($sql);
// 將附帶參數(shù)帶入pdo實例
$this->bindValues($statement, $bindings);
// 執(zhí)行
$statement->execute();
// 返回所有合法數(shù)據(jù), 以O(shè)bject對象為數(shù)據(jù)類型
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)
上一期 如何寫一個屬于自己的數(shù)據(jù)庫封裝(3) - 查詢 - 入門篇
下一期 如何寫一個屬于自己的數(shù)據(jù)庫封裝(5) - 查詢 - JOIN篇