如何在symfony 2 php的注释中访问类变量和常量

2022-01-03 00:00:00 php symfony doctrine-orm

我有一堂这样的课:

class Student {

const GENDER_MALE = "male", GENDER_FEMALE = "female";
/**
 * @var string $gender
 *
 * @ORMColumn(name="gender", type="string", length=50,nullable=false)
 * @AssertNotBlank(message="Gender cannot be blank",groups={"new"})
 * @AssertChoice(choices = {"male", "female"}, message = "Choose a valid gender.", groups={"new"})
 */
private $gender;

我必须对 "male""female" 值进行硬编码.有可能做这样的事情吗?

I have to hard code the values "male" and "female". Is it possible to do something like this ?

选择 = {self::GENDER_MALE, self::GENDER_FEMALE}

choices = {self::GENDER_MALE, self::GENDER_FEMALE}

推荐答案

这是 Doctrine2 注释阅读器(常量).

您的解决方案:

class Student
{
    const GENDER_MALE = "male", GENDER_FEMALE = "female";

    /**
     * @var string $gender
     *
     * @ORMColumn(name="gender", type="string", length=50,nullable=false)
     * @AssertNotBlank(message="Gender cannot be blank",groups={"new"})
     * @AssertChoice(
     *      choices = {
     *          Student::GENDER_FEMALE: "Female",
     *          Student::GENDER_MALE: "Male"
     *      },
     *      message = "Choose a valid gender.", groups={"new"}
     * )
     */
    private $gender;
}

相关文章