Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7999807
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:36:30+00:00 2026-06-04T15:36:30+00:00

This is a followup question from my original question: Best strategy for migrating mysql

  • 0

This is a followup question from my original question:
Best strategy for migrating mysql enums to doctrine entities with symfony2?

I have been successful at adding an enum data type following the instructions here: http://docs.doctrine-project.org/projects/doctrine-orm/en/2.0.x/cookbook/mysql-enums.html
More specifically I used the solution 2 using the latter part where I created a base EnumType Class and then extend it like:

<?php
namespace CP\AdminBundle\DataTypes;

class EnumContactBasicGender extends EnumType
{
    protected $name = 'EnumContactBasicGender';
    protected $values = array('m','f');

}

My EnumType class looks like:

<?php
namespace CP\AdminBundle\DataTypes;

use Doctrine\DBAL\Types\Type;
use Doctrine\DBAL\Platforms\AbstractPlatform;

abstract class EnumType extends Type
{
    protected $name;
    protected $values = array();

    public function getSqlDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
    {
        $values = array_map(function($val) { return "'".$val."'"; }, $this->values);

        return "ENUM(".implode(", ", $values).") COMMENT '(DC2Type:".$this->name.")'";
    }

    public function convertToPHPValue($value, AbstractPlatform $platform)
    {
        return $value;
    }

    public function convertToDatabaseValue($value, AbstractPlatform $platform)
    {
        if (!in_array($value, $this->values)) {
            throw new \InvalidArgumentException("Invalid enum value given: '".$this->value."' for enum: '".$this->name."'");
        }
        return $value;
    }

    public function getName()
    {
        return $this->name;
    }
}

And then I registered my enums like this:

<?php

namespace CP\AdminBundle;
use Doctrine\DBAL\Types\Type;
use Symfony\Component\HttpKernel\Bundle\Bundle;

class CPAdminBundle extends Bundle
{

 public function boot()
    {
        $em = $this->container->get('doctrine.orm.entity_manager');
        Type::addType('EnumContactBasicGender', 'CP\AdminBundle\DataTypes\EnumContactBasicGender');
        Type::addType('EnumContactBasicType', 'CP\AdminBundle\DataTypes\EnumContactBasicType');
        $em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('EnumContactBasicGender','EnumContactBasicGender');
        $em->getConnection()->getDatabasePlatform()->registerDoctrineTypeMapping('EnumContactBasicType','EnumContactBasicType');
    }
}

When I run the command ./app/console doctrine:generate:entities CP it works! It creates my entities with the new enum data types, it creates the setters and getters with no problem, one of my entities looks like this:

<?php
namespace CP\AdminBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;


/**
 * CP\AdminBundle\Entity\ContactBasic
 *
 * @ORM\Table(name="contacts_basics")
 * @ORM\Entity(repositoryClass="CP\AdminBundle\Entity\ContactBasicRepository")
 */
class ContactBasic
{
    /** Field #1 (contacts_id) **/
    /**
    * @var integer $contacts_id
    * @ORM\Column(type="integer",nullable=false)
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="AUTO")
    */
    private $contacts_id;
    /** enum ('m','f') **/
    /**
    * @ORM\Column(type="EnumContactBasicGender",nullable=false)
    */
    private $gender;
    /** enum ('non-customer','customer','blacklisted') **/
    /**
    * @ORM\Column(type="EnumContactBasicType",nullable=false)
    */
    private $type;

        /** more omitted properties here **/

    /**
     * Set gender
     *
     * @param EnumContactBasicGender $gender
     */
    public function setGender(\EnumContactBasicGender $gender)
    {
        $this->gender = $gender;
    }

    /**
     * Get gender
     *
     * @return EnumContactBasicGender 
     */
    public function getGender()
    {
        return $this->gender;
    }
    /**
     * Set type
     *
     * @param EnumContactBasicType $type
     */
    public function setType(\EnumContactBasicType $type)
    {
        $this->type = $type;
    }

    /**
     * Get type
     *
     * @return EnumContactBasicType 
     */
    public function getType()
    {
        return $this->type;
    }

        /** more omitted setters and getters here **/
}

My problem comes when I create a fixture loader, I do not know how to set any of the enum values to get them saved to the database, so far my fixture loader class looks like this:

<?php
namespace CP\AdminBundle\DataFixtures\ORM;

use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use CP\AdminBundle\Entity\ContactBasic;
use CP\AdminBundle\DataTypes\EnumContactBasicGender;
use CP\AdminBundle\DataTypes\EnumContactBasicType;


class ContactBasicLoader implements FixtureInterface
{
    public function load(ObjectManager $manager)
    {


        $contact = new ContactBasic();
      //  $contact->setGender(new EnumContactBasicGender());
        $contact->setGender('m');

       // $contact->setType(new EnumContactBasicType());
        $contact->setType('customer');

        $manager->persist($contact);
        $manager->flush();




    }
}

When I run ./app/console doctrine:fixtures:load I get the following error:

Catchable Fatal Error: Argument 1 passed to CP\AdminBundle\Entity\ContactBasic::setGender() must be an instance of EnumContactBasicGender, string given

but if I pass the class like the commented lines above I also get an error.

Can someone please explain how I need to set the values needed?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T15:36:31+00:00Added an answer on June 4, 2026 at 3:36 pm

    I faced a very similar issue, I solved by adding in the XXXEnumType class the same array of values but public and static, in this way I specified in a single place the content of my enum, access it from every where and foreach it 🙂

    But first, check if you added my_foo_type in app/config/config.yml:

    # Doctrine Configuration
    doctrine:
        dbal:
            driver:   %database_driver%
            host:     %database_host%
            port:     %database_port%
            dbname:   %database_name%
            user:     %database_user%
            password: %database_password%
            charset:  UTF8
            types:
              my_foo_type:      use XXX\DBAL\XXXEnumType
            mapping_types:
              enum: string
    

    <?php
    namespace XXX\DBAL;
    
    class XXXEnumType extends EnumType
    {
        protected $name = 'enum_xxx';
        protected $values = array('first', 'second', 'last');
    
        public static $static_name = 'enum_xxx';
        public static $static_values = array('first', 'second', 'last');
    
        public function getValues()
        {
            return $this->values;
        }
    }
    

    and from elsewhere in my project I can do:

    use XXX\DBAL\XXXEnumType;
    

    foreach (XXXEnumType::$static_values as $value)
    {
        //do some stuff
    }
    

    Hope it helps,

    Linuxatico

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a followup to my last question . I now have a byte[]
This Question arises from a Q&A here I have some doubts that i think
This is a followup question from this one. Connecting Pyside with matplotlib My PythonFu
This is a followup question of How to encode characters from Oracle to Xml?
This is a followup to this question: How to wait for input from the
Followup question from this one: Swing font names do not match? (Making a font
I have a followup question to this question . I'm writing a web service
A followup to this previous question: Current code: var query = from b in
this is a followup question to Remove default apps from Django-admin I am trying
This is a followup to a question I asked yesterday: Have you ever had

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.