数据库访问抽象类

发布于 2024-09-13 01:34:20 字数 1249 浏览 7 评论 0原文

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

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

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

谢谢!

Currently, I have a database access class named DB, which uses PDO. I Then have a handful of sub-classes for accessing each table:

  • Users
  • Images
  • Galleries
  • Text
  • Videos

This was nice when I first started my project, but now I'm not sure if it's that great, as I have a method for each database query that I use within each of these classes. For example:

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)

Well, you get the idea. I have been adding these methods as the need for them arises, and in development, I start by just using the DB class:

//Execute a query
DB::query($query);

//Get a single row
$row = DB::getSingleRow($query);

//Get multiple rows
$rows = DB::getMultipleRows($query);

So, I test queries with my DB class, then when they are working, I wrap them in a method of the class that is related to it (Image class for images table, Galleries class for galleries table, etc.).

I feel like this will keep growing and growing as I add new features later on (which may be OK, but I'm not certain). Can anybody critique my method and/or provide alternative solutions?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铜锣湾横着走 2024-09-20 01:34:20

不,这实际上听起来不错。您似乎在业务逻辑和数据访问之间有一个坚实的抽象层(这称为 数据映射器模式)。

规模变大的唯一问题是,您最终可能会得到重叠的方法。您还应该尝试在这两个类之间维护标准命名约定。

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

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

No, this actually sounds pretty good. You seem to have a solid abstraction layer between business logic and data access (This is called Data Mapper pattern).

The only problem with this getting big is, that you might end up with methods that overlap. You should also try to maintain a standard naming convenction across both classes.

In Images the method is Images::insertNew and in Galleries it is Galleries:createNew.

Do you actually have models? Because what it looks like is that you have a lot of queries to assemble single values, but not whole objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文