I for my part like using Entity Constats
for simple relations. Let's say for example you have an attribute of your entity called status
that can only have values between 0-2. An extra status
entity might be overkill in this scenario so I simply define them as constants
:
class Entity {
const STATUS_PUBLIC = 0;
const STATUS_WAITING = 1;
const STATUS_REJECTED = 2;
}
It allows you to simply check for a status
by comparing to constants:
$entity->getStatus() == Entity::STATUS_PUBLIC
Or even in twig:
entity.status == constant('STATUS_PUBLIC', entity)
Or in a QueryBuilder
:
$builder->andWhere('status = :status')
->setParameter('status', Entity::STATUS_PUBLIC)
It has proven to be quite effective for me.
As other answers point out for global constants should definitely be avoided. Use parameters for that.
As it seems to be unclear when to use Parameters
:
Taken from the official best practice:
Creating a configuration option for a value that you are never going to configure just isn't necessary.
You might consider using parameters though if these values change. One example would be your application running in multiple environments. Using parameters will give you the power to adapt to environment changes without updating your code base.