首页 文章

如何为文档中的Symfony 2.7选择实现ENUM类?

提问于
浏览
3

在Symfony 2.7中浏览Choice表单字段界面的文档时,我会在top of this page注意以下代码段:

$builder->add('attending', 'choice', array(
    'choices' => array(
        Status::getInstance(Status::YES),
        Status::getInstance(Status::NO),
        Status::getInstance(Status::MAYBE),
    ),
    'choices_as_values' => true,
    'choice_label' => 'displayName',
));

Status 本质上是在PHP中实现一个枚举类 . 在Symfony中似乎没有相关的接口 . 有谁知道如何优雅地实现像 Status 这样的东西,并允许在一个地方添加更多的值?

2 回答

  • 1
    class Status {
    
        const STATUS_YES = 1;
        const STATUS_MAYBE = 2;
        const STATUS_NO = 3;
    
        private $enum;
    
        static public function getInstance($var)
        {
            return new static($var);
        }
    
        private function __construct($var)
        {
            $this->enum = $var;
        }
    
        /**
         * @returns boolean
         */
        public function is($var)
        {
            return ($this->enum == $var);
        }
    }
    

    如果需要,可以使用Status作为抽象类,从该类继承所有Status并返回相应的对象

  • 0

    通过交响乐,我们可以使用关于您的实体的注释和学说关怀 . 别忘了将'Doctrine \ ORM \ Mapping称为ORM'

    你应该有更多关于symfony docs的信息:http://symfony.com/blog/symfony2-annotations-gets-better

    use Doctrine\ORM\Mapping as ORM;
    use Doctrine\Common\Collections as Collection;
    
    /**
     * Class Person
     *
     * @package MyPackage\MyBundle\Entity
     *
     * @author Mirouf
     *
     * @ORM\Table(name="t_persons")
     * @ORM\Entity(repositoryClass="MyPackage\MyBundle\Repository\PersonRepository")
     * @ORM\HasLifecycleCallbacks()
     */
    
    class Person {
        const TYPE_DIVORCED = 'divorced';
        const TYPE_MARRIED = 'married';
        const TYPE_SINGLE = 'single';
    
        /**
         * @var integer
         *
         * @ORM\Column(name="id", type="integer", options={"comment"="Id of the person"})
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
    
         /**
          * @var string
          *
          * @ORM\Column(name="name", type="string", length=35, options={"comment"="Name of the person"})
          */
    
         private $number;
         /**
          * @var string
          *
          * @ORM\Column(name="relationshipStatus", nullable=true, options={"comment"="relationshipStatus"}, columnDefinition="ENUM('divorced', 'married', 'single')")
          */
        private $relationshipStatus;
    
        /** 
         * Get relationshipStatus
         *
         * @return string 
        */
        public function getRelationshipStatus()
        {
            return $this->relationshipStatus;
        }
    
        /**
         * Set relationshipStatus
         *
         * @param string $relationshipStatus
         *
         * @return Person
         */
        public function setRelationshipStatus($relationshipStatus)
        {
            $this->relationshipStatus = $relationshipStatus;
    
            return $this;
        }
    }
    

    我之前没有看到过这个帖子有同样的问题,所以如果我能帮助别人的话!

相关问题