4

在我的一个实体中,我添加了这个:

class MyClass {

  // Annotations   

  /**
   * @ORM\Column(type="integer")
   */
  private $status;

  // Status values
  const Created = 10;
  const Refused = 20;
  const Valid   = 30;

  // Getters, setters
}

所以我可以像枚举一样使用 MyClass::Status (MyClass::Created, MyClass::Refused etc...) 访问这些值。

我想检查我的实体在模板中的当前状态。但我不知道该怎么做。

我试过(绝望地):

{% if entity.status == entity.Created %}

哪个不按预期工作。

但没有任何效果,我在谷歌或 SO 上也没有找到任何东西。

4

3 回答 3

4

Created不是您实体的属性,

尝试使用,

{% if entity.status is constant('path_to_your_bundle\\Entity\\MyClass::Created') %}
于 2013-07-18T08:59:23.487 回答
4

更优雅的解决方案(如在 Twig 文档中找到,感谢 Ahmed Siouani)是:

{% if entity.status is constant('Created', entity) %}
于 2013-07-18T09:08:42.497 回答
0

如果您使用的是Backed Enum,您可以像这样访问它的值:

{% if entity.status.value == 'created' %}

于 2022-02-17T01:17:11.107 回答