在数据库中存储类常量(用作位掩码)?

发布于 2024-08-27 07:20:32 字数 1222 浏览 8 评论 0原文

假设我有一个名为 Medium 的类,它可以表示不同类型的媒体。例如:

  • 上传的视频
  • 嵌入的视频
  • 上传的图像
  • 嵌入的图像

我用内容表示这些类型,如下所示:

class MediumAbstract
{
    const UPLOAD       = 0x0001;
    const EMBED        = 0x0010;
    const VIDEO        = 0x0100;
    const IMAGE        = 0x1000;

    const VIDEO_UPLOAD = 0x0101; // for convenience
    const VIDEO_EMBED  = 0x0110; // for convenience
    const IMAGE_UPLOAD = 0x1001; // for convenience
    const IMAGE_EMBED  = 0x1010; // for convenience

    const ALL          = 0x1111; // for convenience
}

因此,我很容易在(抽象)存储库上对它们进行组合搜索,例如:

{
    public function findAllByType( $type )
    {
        ...
    }
}

$media = $repo->findAllByType( MediumAbstract::VIDEO | MediumAbstract::IMAGE_UPLOAD );
// or
$media = $repo->findAllByType( MediumAbstract::ALL );
// etc..

你感觉如何关于在数据库等具体存储库中使用这些常量值?可以吗?或者我应该用数据库中有意义的数据替换它们。

Table medium:

| id |                type | location    | etc..
-------------------------------------------------
|  1 | use constants here? | /some/path  | etc..

(当然,我只会使用有意义的常量:VIDEO_UPLOAD、VIDEO_EMBED、IMAGE_UPLOAD 和 IMAGE_EMBED)

Let's say I have a class called Medium which can represent different types of media. For instance:

  • uploaded video
  • embedded video
  • uploaded image
  • embedded image

I represent these types with contants, like this:

class MediumAbstract
{
    const UPLOAD       = 0x0001;
    const EMBED        = 0x0010;
    const VIDEO        = 0x0100;
    const IMAGE        = 0x1000;

    const VIDEO_UPLOAD = 0x0101; // for convenience
    const VIDEO_EMBED  = 0x0110; // for convenience
    const IMAGE_UPLOAD = 0x1001; // for convenience
    const IMAGE_EMBED  = 0x1010; // for convenience

    const ALL          = 0x1111; // for convenience
}

Thus, it is easy for me to do a combined search on them on an (abstract) repository, with something like:

{
    public function findAllByType( $type )
    {
        ...
    }
}

$media = $repo->findAllByType( MediumAbstract::VIDEO | MediumAbstract::IMAGE_UPLOAD );
// or
$media = $repo->findAllByType( MediumAbstract::ALL );
// etc..

How do you feel about using these constant values in a concrete repository like a database? Is it ok? Or should I substitute them with meaningful data in the database.

Table medium:

| id |                type | location    | etc..
-------------------------------------------------
|  1 | use constants here? | /some/path  | etc..

(Of course I'll only be using the meaningful constants: VIDEO_UPLOAD, VIDEO_EMBED, IMAGE_UPLOAD and IMAGE_EMBED)

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

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

发布评论

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

评论(1

許願樹丅啲祈禱 2024-09-03 07:20:32

在 mySQL 中,我会使用 SET。它们使用最少量的数据在内部存储,但为您提供值的预定义明文表示:

SET('UPLOAD','EMBED','VIDEO','IMAGE'); // costs 1 byte

SET 文档

您可以应用数据库查询:SELECT * FROM table WHERE type IN ("UPLOAD", "EMBED")

如果您使用它,让常量与字符串值匹配可能会更容易:(

class MediumAbstract
{
    const UPLOAD       = "UPLOAD";
    const EMBED        = "EMBED";
    const VIDEO        = "VIDEO";
    const IMAGE        = "IMAGE";

您现在正在做的事情: 0x0001 无论如何都不是位掩码,而是十六进制值,不是吗?更多 < a href="https://stackoverflow.com/questions/2507208/whats-the-prefix-for-binary-in-php">此处)

In mySQL, I would use SET. They are stored internally using the smallest amount of data, but give you a predefined clear text representation of the value:

SET('UPLOAD','EMBED','VIDEO','IMAGE'); // costs 1 byte

Documentation on SET

You can apply database queries: SELECT * FROM table WHERE type IN ("UPLOAD", "EMBED")

If you use that, it would probably be easier to make the constants match the string values:

class MediumAbstract
{
    const UPLOAD       = "UPLOAD";
    const EMBED        = "EMBED";
    const VIDEO        = "VIDEO";
    const IMAGE        = "IMAGE";

(What you're doing right now: 0x0001 is not a bit mask anyway, but a hexadecimal value, isn't it? More here)

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