本文介紹了表數(shù)據(jù)網(wǎng)關(guān)的定義與作用,它是一種設(shè)計(jì)模式,用于在應(yīng)用程序和數(shù)據(jù)庫(kù)之間傳輸數(shù)據(jù)。文章還詳細(xì)解釋了網(wǎng)關(guān)類(lèi)的功能以及其在應(yīng)用程序中的應(yīng)用場(chǎng)景。閱讀本文能夠?qū)Ρ頂?shù)據(jù)網(wǎng)關(guān)有更深入的了解,并了解它如何與數(shù)據(jù)庫(kù)交互以及在業(yè)務(wù)類(lèi)中的應(yīng)用。
什么是表數(shù)據(jù)網(wǎng)關(guān)?
表數(shù)據(jù)網(wǎng)關(guān)是一種設(shè)計(jì)模式,它由在應(yīng)用程序和數(shù)據(jù)庫(kù)之間傳輸數(shù)據(jù)的類(lèi)表示。因此,類(lèi)只有持久化方法作為操作,即數(shù)據(jù)記錄。
我們還將有另一個(gè)類(lèi),它是應(yīng)用程序的業(yè)務(wù)類(lèi),每當(dāng)需要在數(shù)據(jù)庫(kù)中搜索或保存數(shù)據(jù)時(shí)就使用網(wǎng)關(guān)類(lèi)。
在大多數(shù)情況下,表數(shù)據(jù)網(wǎng)關(guān)處理關(guān)系模型,與數(shù)據(jù)庫(kù)的主表具有 1:1 的關(guān)系。
例子
第 1 步 - 目錄系統(tǒng):
??Table_Data_Gateway ┣ ??class ┃ ┣ ??Product.php ┃ ┗ ??ProductGateway.php ┣ ??config ┃ ┗ ??config.ini ┣ ??database ┃ ┗ ??product.db ┗ ??index.php
第 2 步 - 數(shù)據(jù)庫(kù)配置文件:
host = name = database/product.db user = pass = type = sqlite
第 3 步 - 數(shù)據(jù)庫(kù):
CREATE TABLE product( id INTEGER PRIMARY KEY NOT NULL, description TEXT, stock FLOAT, cost_price FLOAT, sale_price FLOAT, bar_code TEXT, date_register DATE, origin CHAR(1) );
第 4 步 - ProductGateway 類(lèi):
<?php class ProductGateway { private static $conn; public function __construct() { } public static function setConnection(PDO $conn) { self::$conn = $conn; } public function find($id, $class = 'stdClass') { $sql = "SELECT * FROM product WHERE id = '$id'"; print "$sql <br>"; $result = self::$conn->query($sql); return $result->fetchObject($class); } public function all($filter = '', $class = 'stdClass') { $sql = "SELECT * FROM product"; if( $filter ) { $sql .= " WHERE $filter"; } print "$sql <br>"; $result = self::$conn->query($sql); return $result->fetchAll(PDO::FETCH_CLASS, $class); } public function delete($id) { $sql = "DELETE FROM product WHERE id = '$id'"; print "$sql <br>"; return self::$conn->query($sql); } public function save($data) { if( empty($data->id) ) { $sql = "INSERT INTO product (description, stock, cost_price, sale_price, bar_code, date_register, origin) VALUES ('{$data->description}',{$data->stock},{$data->cost_price},{$data->sale_price}, {$data->bar_code},'{$data->date-register}','{$data->origin}')"; } else { $sql = "UPDATE product SET description = '{$data->description}', stock = '{$data->stock}', cost_price = '{$data->cost_price}', sale_price = '{$data->sale_price}', bar_code = '{$data->bar_code}', date_register = '{$data->date_register}', origin = '{$data->origin}' WHERE id = '{$data->id}'"; } print "$sql <br>"; return self::$conn->exec($sql); } }
第 5 步 - 產(chǎn)品類(lèi)別:
<?php class Product { private $data; public function __construct() { } public static function setConnection( PDO $conn) { ProductGateway::setConnection($conn); } public function __get($prop) { return $this->data[$prop]; } public function __set($prop, $value) { $this->data[$prop] = $value; } public static function find($id) { $gw = new ProductGateway; return $gw->find($id, 'Product'); } public static function all($filter = '') { $gw = new ProductGateway; return $gw->all($filter, 'Product'); } public function save() { $gw = new ProductGateway; return $gw->save( (object) $this->data); } public function delete() { $gw = new ProductGateway; return $gw->delete($this->id); } public function getProfitMargin() { return (($this->sale_price - $this->cost_price)/$this->cost_price)*100; } public function registerPurchase($cost, $quantity) { $this->cost_price = $cost; $this->stock += $quantity; } }
測(cè)試
<?php require_once 'class/Product.php'; require_once 'class/ProductGateway.php'; try { $ini = parse_ini_file('config/config.ini'); $dbname = $ini['name']; $conn = new PDO('sqlite:'.$dbname); $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); Product::setConnection($conn); $product = new Product; } catch(Exception $e) { print $e->getMessage(); }
在數(shù)據(jù)庫(kù)中記錄產(chǎn)品:
$product->description = 'Juice'; $product->stock = 8; $product->cost_price = 12; $product->sale_price = 18; $product->bar_code = '123123123'; $product->origin = 'S'; $product->date_register = date('Y-m-d'); $product->save();
從數(shù)據(jù)庫(kù)更新產(chǎn)品:
$update = $product::find(1); $update->description = "Grape Juice"; $update->save($product);
列出產(chǎn)品:
foreach( $product::all() as $p ) { print $p->description . ' '; print $p->cost_price . ' '; print $p->sale_price . "<br>"; }
經(jīng)營(yíng)方式:
文章來(lái)源:http://www.zghlxwxcb.cn/article/417.html
$p = $product::find(1); $p->registerPurchase(24,2); //(cost,quantity) $p->save($product); print $p->getProfitMargin();
文章來(lái)源地址http://www.zghlxwxcb.cn/article/417.html
到此這篇關(guān)于了解表數(shù)據(jù)網(wǎng)關(guān)的工作原理與應(yīng)用場(chǎng)景 | PHP 設(shè)計(jì)模式:表數(shù)據(jù)網(wǎng)關(guān)的文章就介紹到這了,更多相關(guān)內(nèi)容可以在右上角搜索或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!