0
votes

I have a column in MySQL table defined as follows:

`fuel_type` set('gasoline','diesel','LPG','CNG','ethanol','bio-diesel','hydrogen') DEFAULT NULL,

I generated entities usingn doctrine's database introspection feature. The generated code in the entity in question is this:

 /**
 * @var simplearray
 *
 * @ORM\Column(name="fuel_type", type="simplearray", nullable=true)
 */
private $fuelType;

/**
 * Set fuelType
 *
 * @param \simplearray $fuelType
 * @return NomEngine
 */
public function setFuelType(\simplearray $fuelType)
{
    $this->fuelType = $fuelType;

    return $this;
}

/**
 * Get fuelType
 *
 * @return \simplearray 
 */
public function getFuelType()
{
    return $this->fuelType;
}

In my sonata admin class the configureFormsFields method is thefined this way:

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper                
            ->add('name')
            ->add('fuel_type', 'choice', array(
                'choices' => array(
                    'gasoline' => 'Gasoline',
                    'diesel' => 'Diesel',
                    'LPG' => 'LPG',
                    'CNG' => 'CNG',
                    'ethanol' => 'Ethanol',
                    'bio-diesel' => 'Bio Diesel',
                    'hydrogen' => 'Hydrogen'
                ),
                'multiple' => true,
                'required' => false
            ));               
    ;
}

The problem is that after I try to save record in the database I get this exception:

Unknown column type "simplearray" requested. Any Doctrine type that you use has to be registered with \Doctrine\DBAL\Types\Type::addType(). You can get a list of all the known types with \Doctrine\DBAL\Types\Type::getTypesMap(). If this error occurs during database introspection then you might have forgot to register all database types for a Doctrine Type. Use AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement Type#getMappedDatabaseTypes(). If the type name is empty you might have a problem with the cache or forgot some mapping information.
500 Internal Server Error - DBALException 

I tried a couple of things to resolve this issue:

  1. I noticed, that the generated type is 'simplearray', but in doctrine this type is 'simple_array'. I thought there was a typo. Without success I tried to map simplearray to simple_array in config.yml :

    doctrine:
        dbal:     
            mapping_types:         
                simplearray: simple_array
    
  2. After that I tried to change simplearray to simple_array in the entity. I got this error:

    Catchable Fatal Error: Argument 1 passed to Acme\AdminBundle\Entity\Engine::setFuelType() must be an instance of simple_array, array given,             
    
  3. I thought that the admin class was passing array, and the entity was expecting simple_array, so I changed simple_array to array in the entity. Now the error was this:

    Could not convert database value "" to Doctrine Type array 500 Internal Server Error - ConversionException 
    

Any insights about dealing with set columns in Sonata Admin will be greatly appreciated!

2

2 Answers

1
votes

Your entity setter & getter are wrong too and should deals with a PHP array as Doctrine is converting it, I think you must change them to:

/**
 * Set fuelType
 *
 * @param array $fuelType
 *
 * @return NomEngine
 */
public function setFuelType(array $fuelType)
{
    $this->fuelType = $fuelType;

    return $this;
}

/**
 * Get fuelType
 *
 * @return array
 */
public function getFuelType()
{
    return $this->fuelType;
}
0
votes

It seems that Doctrine doesn't handle well the set syntax in MySQL. I see 2 ways you could solve your issue:

  1. Change your MySQL schema to put in a json array or a varchar instead of your set. Probably the fastest way.
  2. You might not have the luxury to change your schema. In that case, define a custom Doctrine type to suit your needs as described there: http://docs.doctrine-project.org/en/2.0.x/cookbook/mysql-enums.html#solution-2-defining-a-type ; you'll need then to register it to Symfony as explained there: http://symfony.com/doc/current/cookbook/doctrine/dbal.html#registering-custom-mapping-types

Hope it helps!