8000 Doctrine\DBAL\Types\Type::__toString str_replace() bug · Issue #5760 · doctrine/orm · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Doctrine\DBAL\Types\Type::__toString str_replace() bug #5760
Open
@d42ohpaz

Description

@d42ohpaz

When a database type is named using the word 'Type', it will get removed and cause the following error:

Error: Class 'MyAppBundle\Type\TypeAccount' not found

/Users/dohpaz42/Development/MyApp/src/MyAppBundle/Type/TypeAccountType.php:18
/Users/dohpaz42/Development/MyApp/vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/SimpleObjectHydrator.php:128
/Users/dohpaz42/Development/MyApp/vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/SimpleObjectHydrator.php:69
/Users/dohpaz42/Development/MyApp/vendor/doctrine/orm/lib/Doctrine/ORM/Internal/Hydration/AbstractHydrator.php:147
/Users/dohpaz42/Development/MyApp/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php:888
/Users/dohpaz42/Development/MyApp/vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php:181
/Users/dohpaz42/Development/MyApp/vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php:164
/Users/dohpaz42/Development/MyApp/tests/MyAppBundle/Entity/AccountTest.php:29

The following defines my type in code. I've found that if I do not return an instance of the type from convertToPHPValue I do not get the error.

Doctrine config

# Doctrine Configuration
doctrine:
    dbal:
        driver:   pdo_pgsql
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        charset:  UTF8

        types:
            currency:       MyAppBundle\Type\CurrencyType
            type_tx:        MyAppBundle\Type\TypeTransactionType
            acct_equation:  MyAppBundle\Type\AccountEquationType
            type_acct:      MyAppBundle\Type\TypeAccountType
            datetimetz:     Doctrine\DBAL\Types\VarDateTimeType

MyAppBundle\Type\TypeAccountType

<?php
namespace MyAppBundle\Type;

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

class TypeAccountType extends Type
{
    const TYPE_ACCOUNT = 'type_acct';

    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
    {
        return $this::TYPE_ACCOUNT;
    }

    public function convertToPHPValue($value, AbstractPlatform $platform)
    {
        return new TypeAccount($value);
    }

    public function convertToDatabaseValue($value, AbstractPlatform $platform)
    {
        return $value->toTypeAccount();
    }

    public function getName()
    {
        return $this::TYPE_ACCOUNT;
    }
}

I've tracked the error down to Doctrine\DBAL\Types\Type::__toString() in that it blindly removes any instance of the word Type from the class name, when in fact the intent is to remove the last instance of the word Type. Since my class is TypeAccountType, it is being turned into Account, which does not exist and causes the error to be thrown.

Doctrine\DBAL\Types\Type

    /**
     * @return string
     */
    public function __toString()
    {
        $e = explode('\\', get_class($this));

        return str_replace('Type', '', end($e));
    }

I am willing to make a unit test and a PR, but wanted to document and get feedback first.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0