感受比直接弄SQL語句高級,但還不到ORM的封裝。php
一步一步進化。html
app.jsonmysql
{ "db": { "user": "root", "password": "xxxx", "host": "10.2.3.4", "port": "3306", "dbname": "bookstore" } }
config.phpsql
<?php namespace Bookstore\Utils; use Bookstore\Exceptions\NotFoundException; require_once __DIR__ . '/NotFoundException.php'; class Config { private $data; //類靜態變量,保證變量惟一性 private static $instance; //構造函數私有化,類外部不能夠調用. private function __construct() { $json = file_get_contents(__DIR__ . '/app.json'); $this->data = json_decode($json, true); } //單例模式,保證只實例化一個類. public static function getInstance() { if (self::$instance == null) { //是能夠本身實例化本身的. self::$instance = new Config(); } return self::$instance; } public function get($key) { if (!isset($this->data[$key])) { throw new NotFoundException("Key $key not in config."); } return $this->data[$key]; } } ?>
test.phpjson
<?php //使用命名空間,易於在大型應用中管理和組織php類. use Bookstore\Utils\Config; //命名空間能夠直接use,但若是這個命名空間沒有在標準約定位置,且沒有自動載入的話,須要使用require來手工定位一下. require_once __DIR__ . '\Config.php'; header("content-type:text/html;charset=utf-8"); $dbConfig = Config::getInstance()->get("db"); $connStr = "mysql:host={$dbConfig['host']};port={$dbConfig['port']};dbname={$dbConfig['dbname']};charset=utf8"; $db = new \PDO($connStr, $dbConfig['user'], $dbConfig['password']); $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC); $query = 'SELECT * FROM book WHERE author = :author'; $statement = $db->prepare($query); $statement->bindValue('author', 'George Orwell'); $statement->execute(); $rows = $statement->fetchAll(); foreach ($rows as $row) { var_dump($row); } echo "<br/>"; $query = <<<SQL INSERT INTO book(isbn, title, author, price) VALUES(:isbn, :title, :author, :price) SQL; $statement = $db->prepare($query); $params = [ 'isbn' => '9781413108614', 'title' => 'Iliad', 'author' => 'Homer', 'price' => 9.25 ]; $statement->execute($params); $result = $db->exec($query); echo $db->lastInsertId(); echo "<br/>"; function addBook(int $id, int $amount=1):void { $query = 'UPDATE book SET stock = stock + :n WHERE id = :id'; $statement = $db->prepare($query); $statement->bindValue('id', $id); $statement->bindValue('n', $amount); if (!$statement->execute()) { throw new Exception($statement->errorInfo()[2]); } } function addSale($db, int $userId, array $bookIds):void { $db->beginTransaction(); try { $query = 'INSERT INTO sale(customer_id, date)' . 'VALUES(:id, NOW())'; $statement = $db->prepare($query); if (!$statement->execute(['id'=> $userId])) { throw new Exception($statement->errorInfo()[2]); } $saleId = $db->lastInsertId(); $query = 'INSERT INTO sale_book(book_id, sale_id)' . 'VALUES(:book, :sale)'; $statement = $db->prepare($query); $statement->bindValue('sale', $saleId); foreach ($bookIds as $bookId) { $statement->bindValue('book', $bookId); if (!$statement->execute()) { throw new Exception($statement->errorInfo()[2]); } } $db->commit(); } catch (Exception $e) { $db->rollback(); throw $e; } } try { addSale($db, 1, [1, 2, 300]); } catch (Exception $e) { echo 'Error adding sale: ' . $e->getMessage(); } try { addSale($db, 1, [1, 2, 3]); } catch (Exception $e) { echo 'Error adding sale: ' . $e->getMessage(); } ?>
輸出app
array(6) { ["id"]=> string(1) "1" ["isbn"]=> string(13) "9780882339726" ["title"]=> string(4) "1984" ["author"]=> string(13) "George Orwell" ["stock"]=> string(2) "12" ["price"]=> string(3) "8.7" } array(6) { ["id"]=> string(1) "3" ["isbn"]=> string(13) "9780736692427" ["title"]=> string(11) "Animal Farm" ["author"]=> string(13) "George Orwell" ["stock"]=> string(1) "8" ["price"]=> string(4) "4.06" } 0 Error adding sale: Cannot add or update a child row: a foreign key constraint fails (`bookstore`.`sale_book`, CONSTRAINT `sale_book_ibfk_2` FOREIGN KEY (`book_id`) REFERENCES `book` (`id`))