how to access class variables and constants in ann

2019-02-11 16:09发布

I have a class like this:

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;

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

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

1条回答
时光不老,我们不散
2楼-- · 2019-02-11 16:35

This is a feature of Doctrine2 Annotation Reader (Constants).

You solution:

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;
}
查看更多
登录 后发表回答