gpt4 book ai didi

php - 数据库访问抽象类

转载 作者:搜寻专家 更新时间:2023-10-30 20:45:38 26 4
gpt4 key购买 nike

目前,我有一个名为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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com