This forum is in READ-ONLY mode.
You can look around, but if you want to ask a new question, please use Stack Overflow.

[SOLVED] Adding ENUM to doctrine

New topics about Symfony 2 should go here

[SOLVED] Adding ENUM to doctrine

by guyaloni » Mon Oct 10, 2011 5:41 pm

I try to create a database which contains some enum fields.

I followed teh following topic:
http://forum.symfony-project.org/viewtopic.php?f=23&t=37096&p=123341&hilit=doctrine+enum#p123341

My config.yml file:
Code: Select all
# Doctrine Configuration
doctrine:
    dbal:
        default_connection: default
        connections:
            default:
                driver: %database_driver%
                host: %database_host%
                port: %database_port%
                dbname: %database_name%
                user: %database_user%
                password: %database_password%
                charset: UTF8
                mapping_types:
                    enum: string
                    set: string
                    varbinary: string
                    tinyblob: text
               
    orm:
        auto_generate_proxy_classes: %kernel.debug%
        auto_mapping: true


One of the definition files - UserInfo.orm.yml:
Code: Select all
Dilmot\BackendBundle\Entity\UserInfo:
    type: entity
    table: user_info
    id:
        user_id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        first_name:
            type:     string
            length:   255
        last_name:
            type:     string
            length:   255
        displayed_name:
            type:     string
            length:   255
        gender:
            type:     enum
            values:   [male, female, unknown]
            default:  unknown


When I execute php app/console doctrine:generate:entities MyBundle all files are generated fine.
Then I try php app/console doctrine:schema:update --dump-sql and get the following error:
Code: Select all
[Doctrine\DBAL\DBALException]       
  Unknown column type enum requested.


Is it because the way I define the enum field in the yml file?

By the way, I understand that there is no longer something like use_native_enum: true - is it true? Does it mean that enum will be saved in DB as a string??
User avatar
guyaloni
Member
 
Posts: 84
Joined: Fri May 21, 2010 12:49 pm
Location: Spain

Re: Adding ENUM to doctrine

by pécé » Mon Oct 10, 2011 6:12 pm

ENUM support only applies to reverse engineering (mapping and entity generation from existing DB), not for schema generation...
Visit my blog at http://blog.alterphp.com/
pécé
Member
 
Posts: 69
Joined: Fri Apr 29, 2011 3:51 pm

Re: Adding ENUM to doctrine

by guyaloni » Tue Oct 11, 2011 9:29 am

I don't understand why should be a difference between new design and reverse-engineering in this case.
If the issue is generating the SQL queries to create the DB, I can manually edit them and add the enum fields, as long as I know that the behavior of my doctrine will not be harmed by doing that.
Isn't it like that???
User avatar
guyaloni
Member
 
Posts: 84
Joined: Fri May 21, 2010 12:49 pm
Location: Spain

Re: Adding ENUM to doctrine

by pécé » Tue Oct 11, 2011 10:48 am

The config hack you use makes reverse engineering to treat ENUM fiels as if strings. But Doctrine2 doesn't completely implements ENUM manegement as it is not only designed for MySQL (ENUM is a MySQL type).

So you can manually modify the generated SQL script, but if you wanna manage this types in your entities, you have to tweak them as explained here : http://blog.alterphp.com/2011/09/deal-w ... ities.html
Visit my blog at http://blog.alterphp.com/
pécé
Member
 
Posts: 69
Joined: Fri Apr 29, 2011 3:51 pm

Re: Adding ENUM to doctrine

by guyaloni » Tue Oct 11, 2011 12:51 pm

Nice solution, thanks!!
User avatar
guyaloni
Member
 
Posts: 84
Joined: Fri May 21, 2010 12:49 pm
Location: Spain

Re: Adding ENUM to doctrine

by rikiko2 » Tue Oct 11, 2011 1:52 pm

Hi,
I used SmurfyDoctrineEnumBundle
http://symfony2bundles.org/smurfy/Smurf ... EnumBundle
If you have some problem just ask :-)
Rikiko
User avatar
rikiko2
Faithful Member
 
Posts: 268
Joined: Mon Apr 18, 2011 12:49 pm

Re: [SOLVED] Adding ENUM to doctrine

by guyaloni » Tue Nov 08, 2011 10:43 am

Finally I went for this simple solution:
http://blog.alterphp.com/2011/09/deal-with-enum-fields-in-your-entities.html

I added my small modifications, for those who are interested here it is:
Code: Select all
class Banners
{
   //ENUM of column banner_status
    const BANNER_STATUS_DRAFT     = 'Draft';
    const BANNER_STATUS_PUBLISHED = 'Published';
    const BANNER_STATUS_DELETED   = 'Deleted';
   
    static private $bannerStatusArray = null;
   
    static public function getBannerStatusChoices()
    {
        // Build bannerStatusArray if this is the first call
        if (self::$bannerStatusArray == null)
        {
            self::$bannerStatusArray = array();
            $oClass         = new \ReflectionClass ('\MyCompany\ExampleBundle\Entity\Banner');
            $classConstants = $oClass->getConstants ();
            $constantPrefix = "BANNER_STATUS_";
            foreach ($classConstants as $key=>$val)
            {
                if (substr($key, 0, strlen($constantPrefix)) === $constantPrefix)
                {
                    self::$bannerStatusArray[$val] = $val;
                }
            }
        }
        return self::$bannerStatusArray;
    }

        :
        :

    public function setBannerStatus($bannerStatus)
    {
       if (!in_array($bannerStatus, self::getBannerStatusChoices()))
       {
            throw new \InvalidArgumentException(
                sprintf('Invalid value for Banner.bannerStatus : %s.', $bannerStatus)
            );
       }
        $this->banner_status = $bannerStatus;
    }

If find this code more agile in terms of adding more choices to the enum field - you simply need to add a new constant starting with the same prefix, the rest is done automatically.
User avatar
guyaloni
Member
 
Posts: 84
Joined: Fri May 21, 2010 12:49 pm
Location: Spain

Re: [SOLVED] Adding ENUM to doctrine

by pécé » Tue Nov 08, 2011 1:52 pm

Thanks for this enhancement !

I'll modify my post and mention your modification ;)
Visit my blog at http://blog.alterphp.com/
pécé
Member
 
Posts: 69
Joined: Fri Apr 29, 2011 3:51 pm

Re: [SOLVED] Adding ENUM to doctrine

by guyaloni » Thu Nov 10, 2011 11:30 am

Great, always nice to see that an idea I have help someone... :)
User avatar
guyaloni
Member
 
Posts: 84
Joined: Fri May 21, 2010 12:49 pm
Location: Spain

Re: [SOLVED] Adding ENUM to doctrine

by rikiko2 » Mon Jan 23, 2012 5:22 pm

Great!
it is a very nice solution.
Just to complete the solution (for newby like me) it is necessary to deal fiend as a choiche field:
in Form/EntityType:

Code: Select all
class EntityType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
            ->add('position')
           
            ->add('enumField', 'choice',array(
                'choices'=>\yourBundlePath\Entity\YourEntity::getBannerStatusChoices(),
                ))       


However there is a problem (BE CAREFULL!!!!):
running the following command
./app/console doctrine:schema:create --dump-sql

Symfony (or doctrine) deal with this field type as a "string".


CREATE TABLE YourEntity (... , enumType VARCHAR(255) NOT NULL,... ) ENGINE = InnoDB
User avatar
rikiko2
Faithful Member
 
Posts: 268
Joined: Mon Apr 18, 2011 12:49 pm

Re: [SOLVED] Adding ENUM to doctrine

by althaus » Mon Jan 23, 2012 7:16 pm

Please read the official Doctrine doc on this topic: http://www.doctrine-project.org/docs/or ... enums.html
althaus
Faithful Member
 
Posts: 605
Joined: Tue Oct 18, 2011 2:20 pm
Location: Germany