Doctrine2 conversion error

2020-05-20 07:23发布

What does this error from "Doctrine2 & Symfony2" mean?

Could not convert database value "" to Doctrine Type array

4条回答
我命由我不由天
2楼-- · 2020-05-20 07:47

your db tables column type (eg: longtext) can't match Doctrine's column type. change column type.

查看更多
等我变得足够好
3楼-- · 2020-05-20 07:58

You probably changed a field from type string to type array in your entity but already have data the database. It's failing at trying to convert an empty string from the database to an array.

If it's a development database, simply delete it and create it again, or just delete the offending rows. Or you could convert all the empty strings to a:0:{} (a serialized empty array).

UPDATE table SET column="a:0:{}" WHERE column = "";
查看更多
仙女界的扛把子
4楼-- · 2020-05-20 07:58

I would prefer not having everyone running SQL on their Production Database.

 @ORM\Column(type="array", nullable=TRUE)

An easier solution in make the Column nullable, so after you run your "console doctrine:schema:update --force" the existing DB entries will get a NULL value, instead of an empty string. And doctrine can handle a converting the database value NULL to Doctrine Type array. It should be just a NULL array reference. And PHP empty() doesn't care if its zero sized array or NULL.

In MySQL, I get the following sql-dump:

ALTER TABLE my_table ADD my_new_column LONGTEXT DEFAULT NULL COMMENT '(DC2Type:array)'
查看更多
何必那么认真
5楼-- · 2020-05-20 07:58

You can add new Type like that:

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ArrayType;

class HandicappedArrayType extends ArrayType
{
    public function convertToPHPValue($value, AbstractPlatform $platform)
    {
        if ($value === null || $value === '') {
            return null;
        }

        return parent::convertToPHPValue($value, $platform);
    }

查看更多
登录 后发表回答