DDC-3825: simple_array slush with empty array
Jira issue originally created by user bigfootdd:
I'm using "doctrine/orm": v2.5.0 and have simple_array type. If I persist the class where the property with type simple_array is empty array() the query fails.
I'm coming with solution, please see the github PR https://github.com/doctrine/dbal/pull/877
Comment created by bigfoot:
I've mapped the field as nullable=false, so it should never be null. If you cannot merge this quickly, you should at least add an warning in the docs saying that the field must be mapped set as nullable.
As alternative, I put filter in getter and setter methods:
public function setFields(array $values): void
{
$this->fields = array_filter($values) ?: [null];
}
public function getFields(): array
{
return array_filter($this->fields);
}
Note: It obviously doesn't work if you want to store empty values
Alternatively you can create a custom type based on the "simple_array", it enforce returning '' (empty string) in place of null:
<?php
namespace App\DBAL\Types;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\SimpleArrayType;
class SimpleArrayNotNullType extends SimpleArrayType
{
public function convertToDatabaseValue($value, AbstractPlatform $platform)
{
$value = parent::convertToDatabaseValue( $value, $platform );
if (null === $value) {
$value = '';
}
return $value;
}
public function convertToPHPValue($value, AbstractPlatform $platform)
{
if ('' === $value) {
return [];
}
return parent::convertToPHPValue( $value, $platform );
}
public function getName()
{
return 'simple_array_not_null';
}
}