【问题标题】:Unable to deal with MySQL column of type "SET" in Sonata Admin无法在 Sonata Admin 中处理“SET”类型的 MySQL 列
【发布时间】:2014-01-20 20:35:44
【问题描述】:

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

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

我使用教义的数据库自省功能生成了实体。有问题的实体中生成的代码是这样的:

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

在我的奏鸣曲管理类中,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. 我认为管理类传递数组,而实体期待 simple_array,所以我在实体中将 simple_array 更改为数组。 现在错误是这样的:

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

任何有关在 Sonata Admin 中处理集合列的见解将不胜感激!

【问题讨论】:

    标签: symfony symfony-sonata


    【解决方案1】:

    您的实体 setter 和 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;
    }
    

    【讨论】:

      【解决方案2】:

      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

      希望对你有帮助!

      【讨论】:

        猜你喜欢
        • 2019-01-12
        • 2014-03-09
        • 1970-01-01
        • 2017-01-25
        • 2014-04-27
        • 2016-10-02
        • 2021-07-21
        • 2013-07-01
        • 2017-02-12
        相关资源
        最近更新 更多