how to generate entities and schemas for enum in symfony

30,748

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.yml file.

More information about enum in doctrine here.

Share:
30,748
Viraj.S
Author by

Viraj.S

Updated on July 09, 2022

Comments

  • Viraj.S
    Viraj.S almost 2 years

    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

    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;
    }
    }
    
  • Viraj.S
    Viraj.S about 11 years
    After changing according to your suggestion it's shows following error. [Doctrine\ORM\Mapping\MappingException] Class "Banner\TestBundle\Entity\Contact" is not a valid entity or mapped super class. doctrine:generate:entities [--path="..."] [--no-backup] name
  • Pierrickouw
    Pierrickouw about 11 years
    Try to remove length from the phone annotations. You cant specify a length to an integer
  • Viraj.S
    Viraj.S about 11 years
    Done i have make changes in anotation file and it's working fine now. Thanks for help
  • Acyra
    Acyra almost 10 years
    You can also map to 'varchar' per the docs. docs.doctrine-project.org/en/2.0.x/cookbook/mysql-enums.html I have no idea which is better.
  • Caslav Sabani
    Caslav Sabani almost 8 years
    Thanks man. This was a life saver at 4:33 am in the morning :)