无法处理Sonata Admin中“SET”类型的MySQL列

时间:2014-01-03 08:03:21

标签: symfony symfony-sonata

我在MySQL表中有一个列定义如下:

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

我使用doctrine的数据库内省功能生成实体。有问题的实体中生成的代码是:

 /**
 * @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;
}

在我的sonata admin类中,configureFormsFields方法是这样定义的:

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
            ));               
    ;
}

问题是,在我尝试在数据库中保存记录后,我得到了这个例外:

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 

我尝试了几个方法来解决这个问题:

  1. 我注意到,生成的类型是'simplearray',但在学说中,这种类型是'simple_array'。我以为有一个错字。 如果没有成功,我尝试将simplearray映射到config.yml中的simple_array:

    doctrine:
        dbal:     
            mapping_types:         
                simplearray: simple_array
    
  2. 之后我尝试将simplearray更改为实体中的simple_array。我收到了这个错误:

    Catchable Fatal Error: Argument 1 passed to Acme\AdminBundle\Entity\Engine::setFuelType() must be an instance of simple_array, array given,             
    
  3. 我认为admin类正在传递数组,并且实体期待simple_array,所以我将simple_array更改为实体中的数组。 现在错误是这样的:

    Could not convert database value "" to Doctrine Type array 500 Internal Server Error - ConversionException 
    
  4. 有关处理Sonata Admin中的设置列的任何见解将不胜感激!

2 个答案:

答案 0 :(得分:1)

您的实体制定者& getter也是错的,应该处理一个PHP数组,因为Doctrine正在转换它,我认为你必须将它们改为:

/**
 * 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;
}

答案 1 :(得分:0)

似乎Doctrine在MySQL中没有很好地处理set语法。我看到了两种解决问题的方法:

  1. 将您的MySQL架构更改为放入json数组或varchar而不是您的集合。可能是最快的方式。
  2. 您可能无法更改架构。在这种情况下,定义一个自定义Doctrine类型以满足您的需求,如下所述:http://docs.doctrine-project.org/en/2.0.x/cookbook/mysql-enums.html#solution-2-defining-a-type;然后你需要按照那里的说明将它注册到Symfony:http://symfony.com/doc/current/cookbook/doctrine/dbal.html#registering-custom-mapping-types
  3. 希望它有所帮助!