簡體   English   中英

如何在symfony 2 php中訪問注釋中的類變量和常量

[英]how to access class variables and constants in annotation in 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""female" 可以這樣做嗎?

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

這是Doctrine2 Annotation Reader(常量)的一個功能。

你解決方案:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM