php 如何在 symfony 中为枚举生成实体和模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/14933228/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to generate entities and schemas for enum in symfony
提问by Viraj.S
i am trying to generate entities for my contact information. for that i have first create Entity with the following syntax used where i have created one enum field.
我正在尝试为我的联系信息生成实体。为此,我首先使用以下语法创建实体,其中我创建了一个枚举字段。
php app/console doctrine:generate:entity --entity="BannerTestBundle.contact" --fields="name:string(255) lastname:string(255) phone:integer(10) gender:enum("male","female") message:text".
The above command generate the entity class but when i am trying to generate "entities" from the class it will show error the command is.
上面的命令生成实体类,但是当我尝试从类生成“实体”时,它会显示命令错误。
 php app/console doctrine:generate:entities Banner/TestBundle/Entity/contact
it will show the following error.
它将显示以下错误。
[Doctrine\Common\Annotations\AnnotationException]
[Semantical Error] Couldn't find constant male, property Banner\TestBundle\
Entity\contact::$gender.
doctrine:generate:entities [--path="..."] [--no-backup] name
i want to generate database with following fields:
我想生成具有以下字段的数据库:
Contact.table
Name-string(255)
LastName-string(255)
Phone:integer(10)
gender:enum("male","female")
message:text
please help into it as i am new in symfony
请帮忙解决,因为我是 symfony 的新手
Here is Contact Entity file
这是联系实体文件
 <?php
  namespace Banner\TestBundle\Entity;
   use Doctrine\ORM\Mapping as ORM;
  /**
 * contact
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Banner\TestBundle\Entity\contactRepository")
 */
 class contact
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;
/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=255)
 */
private $name;
/**
 * @var string
 *
 * @ORM\Column(name="lastname", type="string", length=255)
 */
private $lastname;
/**
 * @var enum
 *
 * @ORM\Column(name="gender", type="enum", length=male,female)
 */
private $gender;
/**
 * @var integer
 *
 * @ORM\Column(name="phone", type="integer", length=12)
 */
private $phone;
/**
 * @var string
 *
 * @ORM\Column(name="message", type="text")
 */
private $message;
/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}
/**
 * Set name
 *
 * @param string $name
 * @return contact
 */
public function setName($name)
{
    $this->name = $name;
    return $this;
}
/**
 * Get name
 *
 * @return string 
 */
public function getName()
{
    return $this->name;
}
/**
 * Set lastname
 *
 * @param string $lastname
 * @return contact
 */
public function setLastname($lastname)
{
    $this->lastname = $lastname;
    return $this;
}
/**
 * Get lastname
 *
 * @return string 
 */
public function getLastname()
{
    return $this->lastname;
}
/**
 * Set gender
 *
 * @param \enum $gender
 * @return contact
 */
public function setGender(\enum $gender)
{
    $this->gender = $gender;
    return $this;
}
/**
 * Get gender
 *
 * @return \enum 
 */
public function getGender()
{
    return $this->gender;
}
/**
 * Set phone
 *
 * @param integer $phone
 * @return contact
 */
public function setPhone($phone)
{
    $this->phone = $phone;
    return $this;
}
/**
 * Get phone
 *
 * @return integer 
 */
public function getPhone()
{
    return $this->phone;
}
/**
 * Set message
 *
 * @param string $message
 * @return contact
 */
public function setMessage($message)
{
    $this->message = $message;
    return $this;
}
/**
 * Get message
 *
 * @return string 
 */
public function getMessage()
{
    return $this->message;
}
}
回答by Pierrickouw
Your annotation is not in the right format. Try this :
您的注释格式不正确。尝试这个 :
@ORM\Column(name="gender", type="string", columnDefinition="enum('male', 'femelle')")
And do not forget to add
并且不要忘记添加
mapping_types:
    enum: string
below
以下
doctrine:
     dbal:
        driver:   %database_driver%
        host:     %database_host%
        port:     %database_port%
        dbname:   %database_name%
        user:     %database_user%
        password: %database_password%
        charset:  UTF8
in the app/config/config.ymlfile. 
在app/config/config.yml文件中。
More information about enum in doctrine here.
更多关于 enum 在学说这里的信息。

