Storing class constants (for use as bitmask) in a database?
- by fireeyedboy
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)