Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to access class variables and constants in annotation in symfony 2 php

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}

like image 659
sonam Avatar asked Jun 05 '15 15:06

sonam


1 Answers

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;
}
like image 82
ZhukV Avatar answered Nov 14 '22 07:11

ZhukV