我在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
我尝试了几个方法来解决这个问题:
我注意到,生成的类型是'simplearray',但在学说中,这种类型是'simple_array'。我以为有一个错字。 如果没有成功,我尝试将simplearray映射到config.yml中的simple_array:
doctrine:
dbal:
mapping_types:
simplearray: simple_array
之后我尝试将simplearray更改为实体中的simple_array。我收到了这个错误:
Catchable Fatal Error: Argument 1 passed to Acme\AdminBundle\Entity\Engine::setFuelType() must be an instance of simple_array, array given,
我认为admin类正在传递数组,并且实体期待simple_array,所以我将simple_array更改为实体中的数组。 现在错误是这样的:
Could not convert database value "" to Doctrine Type array 500 Internal Server Error - ConversionException
有关处理Sonata Admin中的设置列的任何见解将不胜感激!
答案 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
语法。我看到了两种解决问题的方法:
希望它有所帮助!