php - 数据库访问抽象类

标签 php database model database-abstraction

目前,我有一个名为DB 的数据库访问类,它使用PDO。然后我有一些子类来访问每个表:

  • 用户
  • 图片
  • 图库
  • 文本
  • 视频

当我第一次开始我的项目时这很好,但现在我不确定它是否那么好,因为我有一个方法用于我在每个类中使用的每个数据库查询。例如:

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);

所以,我用我的数据库类测试查询,然后当它们工作时,我将它们包装在与其相关的类的方法中(图像表的图像类,画廊表的画廊类等)。

我觉得这会随着我以后添加新功能而不断增长(这可能没问题,但我不确定)。有人可以批评我的方法和/或提供替代解决方案吗?

谢谢!

最佳答案

不,这实际上听起来不错。您似乎在业务逻辑和数据访问之间有一个可靠的抽象层(这称为 Data Mapper pattern )。

变大的唯一问题是,您最终可能会使用重叠的方法。您还应该尝试在两个类中保持标准的命名约定。

在 Images 中,方法是 Images::insertNew,在 Galleries 中,方法是 Galleries:createNew。

你们真的有模型吗?因为它看起来像是您有很多查询来组装单个值,而不是整个对象。

关于php - 数据库访问抽象类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3407407/

相关文章:

javascript - 如何使文本输入不可编辑且安全?

mysql - 尝试在现有的 mysql 数据库上构建 Rails 3.2 应用程序

database - 索引组织表和普通表重建索引有什么区别?

sql-server - 使用正则表达式(或类似的)更新字段

c# - CS0411 : The type arguments for method 'System.Web.Mvc.Html.EditorExtensions.EditorFor<>)' cannot be inferred from the usage

model - Tensorflow Slim 恢复模型并预测

asp.net-mvc - 在编辑器模板中设置选择列表的默认选择值

javascript - AngularJS 语法错误 : Unexpected token {

php - 数组复制值到 PHP 中的键

php shell_exec() 在后台运行脚本所需的帮助