如何在symfony 2 php中注释中的类变量和常量 [英] how to access class variables and constants in annotation in symfony 2 php

查看:238
本文介绍了如何在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。有可能做这样的事情吗?

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}

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


推荐答案

这是 Doctrine2注释阅读器(常数)

您的解决方案:

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;
}

这篇关于如何在symfony 2 php中注释中的类变量和常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆