'Accessing enum values in Twig
In one of my entity, I added this :
class MyClass {
// Annotations
/**
* @ORM\Column(type="integer")
*/
private $status;
// Status values
const Created = 10;
const Refused = 20;
const Valid = 30;
// Getters, setters
}
So I can access these values using MyClass::Status (MyClass::Created, MyClass::Refused etc...) like an enumeration.
I want to check what the current status of my entities is in my templates. But I do not know how to do it.
I have tried (hopelessly) :
{% if entity.status == entity.Created %}
Which does not work as expected.
But nothing works, and I did not find anything on google or SO.
Solution 1:[1]
Created is not an attribute of your entity,
Try to use,
{% if entity.status is constant('path_to_your_bundle\\Entity\\MyClass::Created') %}
Solution 2:[2]
If you are using a Backed Enum, you can access its value like this:
{% if entity.status.value == 'created' %}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Ahmed Siouani |
| Solution 2 | Jan Klan |
