orm icon indicating copy to clipboard operation
orm copied to clipboard

DDC-3825: simple_array slush with empty array

Open doctrinebot opened this issue 10 years ago • 3 comments

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

doctrinebot avatar Jul 14 '15 19:07 doctrinebot

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.

doctrinebot avatar Jul 27 '15 08:07 doctrinebot

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

renanbr avatar Jan 18 '18 15:01 renanbr

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

comxd avatar Jan 29 '24 16:01 comxd