![](/img/trans.png)
[英]How to use class constants in @Security annotation using the Symfony Expression Language?
[英]how to access class variables and constants in annotation in symfony 2 php
我有一個這樣的課:
class Student {
const GENDER_MALE = "male", GENDER_FEMALE = "female";
/**
* @var string $gender
*
* @ORM\Column(name="gender", type="string", length=50,nullable=false)
* @Assert\NotBlank(message="Gender cannot be blank",groups={"new"})
* @Assert\Choice(choices = {"male", "female"}, message = "Choose a valid gender.", groups={"new"})
*/
private $gender;
我必須硬編碼"male"
和"female"
。 可以這樣做嗎?
choices = {self :: GENDER_MALE,self :: GENDER_FEMALE}
這是Doctrine2 Annotation Reader(常量)的一個功能。
你解決方案:
class Student
{
const GENDER_MALE = "male", GENDER_FEMALE = "female";
/**
* @var string $gender
*
* @ORM\Column(name="gender", type="string", length=50,nullable=false)
* @Assert\NotBlank(message="Gender cannot be blank",groups={"new"})
* @Assert\Choice(
* choices = {
* Student::GENDER_FEMALE: "Female",
* Student::GENDER_MALE: "Male"
* },
* message = "Choose a valid gender.", groups={"new"}
* )
*/
private $gender;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.