![](/img/trans.png)
[英]What does this mean “PDO provides a data-access abstraction layer” and “PDO does not provide a database abstraction”?
[英]Database access abstraction classes
當前,我有一個名為DB
的數據庫訪問類,它使用PDO
。 然后,我有一些訪問每個表的子類:
Users
Images
Galleries
Text
Videos
當我剛開始我的項目時,這很好,但是現在我不確定它是否很棒,因為我有一個用於這些類中每個數據庫查詢的方法。 例如:
Images::insertNew($filename)
Images::getTotalInGallery($galleryId)
Images::getAllInGallery($galleryId, $fetchStyle)
Images::updateDescription($imageId, $description)
Images::updateGallery($imageId, $galleryId, $orderNum)
Images::getSingle($imageId)
Images::getFilename($imageId)
Images::getImageIdByFilename($filename)
Galleries::getNameById($galleryId)
Galleries::getAll()
Galleries::getMaxImages($galleryId)
Galleries::checkIfExists($galleryId)
Galleries::createNew($galleryName)
Galleries::getById($galleryId)
Galleries::delete($galleryId)
反正你懂這個意思。 我一直在根據需要添加這些方法,在開發中,我僅使用DB
類開始:
//Execute a query
DB::query($query);
//Get a single row
$row = DB::getSingleRow($query);
//Get multiple rows
$rows = DB::getMultipleRows($query);
因此,我用數據庫類測試查詢,然后在查詢工作時,將它們包裝在與其相關的類的方法中(圖像表的Image類,畫廊表的Galleries類,等等)。
我覺得隨着以后添加新功能(這可能還可以,但我不確定),這種情況將不斷增長。 有人可以批評我的方法和/或提供替代解決方案嗎?
謝謝!
不,實際上聽起來不錯。 您似乎在業務邏輯和數據訪問之間有一個牢固的抽象層(這稱為Data Mapper模式 )。
變得越來越大的唯一問題是,您可能最終會得到重疊的方法。 您還應該嘗試在兩個類之間維護標准的命名約定。
在Images中,方法為Images :: insertNew;在Galleries中,方法為Galleries:createNew。
你真的有模特嗎? 因為看起來像是您有很多查詢來組裝單個值,但沒有組裝整個對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.