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

前端之家收集整理的这篇文章主要介绍了如何在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;

我必须硬编码“男”和“女”的价值观.有可能做这样的事情吗?

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

这是 Doctrine2 Annotation Reader (Constants)的一个特点.

你的解决方

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

    /**
     * @var string $gender
     *
     * @ORM\Column(name="gender",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;
}

猜你在找的PHP相关文章