S
S
sorry_i_noob2018-06-01 11:50:36
Database
sorry_i_noob, 2018-06-01 11:50:36

SQL DB - are magic numbers normal in DBs that have constants in the back end code? Or is it better to make a table and a relation?

Hello. I am writing an online store. And now the product has statuses - hidden, shown. Or a tag - hidden, shown, forbidden to be created (some swear word, for example).
In the database, this field is 0 - hidden, 1 - shown, 2 - disabled.
I have a Status class in php and it has constants that expose these values:

const STATUS_HIDDEN = 0;
const STATUS_VISIBLE = 1;
const STATUS_BANNED = 2;

The question is - is it normal that there are constants in php, but in the database they are just numbers (magic numbers) - 0, 1, 2? Do I need to create a table in the database - statuses, where will the statuses be?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
d-stream, 2018-06-01
@d-stream

As practice in living projects has shown, "1,2,3 and will never expand" - it expands quite quickly)
As examples: until recently the gender was M or F and it was possible to hardcore ... already there is no such thing ...
Well, the reference table is good a place to store description variations ("prohibited", "prohibited for sale") for different display cases.
ps By the way, why not be a product shown only to certain groups? (here is the 4th option for what cannot be changed)

R
Rsa97, 2018-06-01
@Rsa97

If no further extension is expected, make the field with ENUM type.

D
Dmitry Entelis, 2018-06-01
@DmitriyEntelis

I think it's normal to have such things entirely in php, and keep just a tinyint in the database

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question