在数据库中存储类常量(用作位掩码)?
假设我有一个名为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 mySQL 中,我会使用 SET。它们使用最少量的数据在内部存储,但为您提供值的预定义明文表示:
SET 文档
您可以应用数据库查询:
SELECT * FROM table WHERE type IN ("UPLOAD", "EMBED")
如果您使用它,让常量与字符串值匹配可能会更容易:(
您现在正在做的事情:
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: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:
(What you're doing right now:
0x0001
is not a bit mask anyway, but a hexadecimal value, isn't it? More here)