Skip to content

Loading…

DBAL-230: Custom types not taken into account when running $ doctrine orm:validate-schema #1406

Closed
doctrinebot opened this Issue · 2 comments

2 participants

@doctrinebot

Jira issue originally created by user maxevron:

Hi,

I created a class to handle PostgreSQL "inet" type and setup bootstape in order to register this new type.

namespace Doctrine\DBAL\Types;

class PostgresInetType extends Type {

    const INET = 'inet'; // modify to match your type name

    public function getSqlDeclaration(array $fieldDeclaration, AbstractPlatform $platform) {
        return 'Inet';
    }

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

    public function convertToDatabaseValue($value, AbstractPlatform $platform) {
        if (null === $value) {
            return null;
        } elseif (preg_match("/^((25[0-5]](2[0-4)[0-9]]([01)?[0-9][0-9]?).(25[0-5]](2[0-4)[0-9]]([01)?[0-9][0-9]?).(25[0-5]](2[0-4)[0-9]]([01)?[0-9][0-9]?).(25[0-5]](2[0-4)[0-9]]([01)?[0-9][0-9]?))](((([0-9A-Fa-f){1,4}:){7}[0-9A-Fa-f]{1,4})]((([0-9A-Fa-f){1,4}:){6}:[0-9A-Fa-f]{1,4})]((([0-9A-Fa-f){1,4}:){5}:([0-9A-Fa-f]{1,4}:)?[0-9A-Fa-f]{1,4})]((([0-9A-Fa-f){1,4}:){4}:([0-9A-Fa-f]{1,4}:){0,2}[0-9A-Fa-f]{1,4})]((([0-9A-Fa-f){1,4}:){3}:([0-9A-Fa-f]{1,4}:){0,3}[0-9A-Fa-f]{1,4})]((([0-9A-Fa-f){1,4}:){2}:([0-9A-Fa-f]{1,4}:){0,4}[0-9A-Fa-f]{1,4})]((([0-9A-Fa-f){1,4}:){6}((b((25[0-5])]((1d{2})|(2[0-4)d)|(d{1,2}))b).){3}(b((25[0-5])]((1d{2})|(2[0-4)d)|(d{1,2}))b))|(([0-9A-Fa-f]{1,4}:){0,5}:((b((25[0-5])]((1d{2})|(2[0-4)d)|(d{1,2}))b).){3}(b((25[0-5])]((1d{2})|(2[0-4)d)|(d{1,2}))b))|(::([0-9A-Fa-f]{1,4}:){0,5}((b((25[0-5])]((1d{2})|(2[0-4)d)|(d{1,2}))b).){3}(b((25[0-5])]((1d{2})|(2[0-4)d)|(d{1,2}))b))|([0-9A-Fa-f]{1,4}::([0-9A-Fa-f]{1,4}:){0,5}[0-9A-Fa-f]{1,4})]((::([0-9A-Fa-f){1,4}:){0,6}[0-9A-Fa-f]{1,4})]((([0-9A-Fa-f){1,4}:){1,7}:))$/", $value)) {
            return $value;
        } else {
            throw new PostgresInetTypeException($value);
        }
    }

    public function getName() {
        return self::INET;
    }

}
// ... other Doctrine setup stuff

// Define Database driver
$objDBALConfig = new \Doctrine\DBAL\Configuration();

$arrConnectionOptions = array(
    'dbname' => 'my*pg*database',
    'user' => 'itsme',
    'host' => '127.0.0.1',
    'driver' => 'pdo_pgsql',
);

$objDBALConnection = DriverManager::getConnection($arrConnectionOptions, $objDBALConfig);

// Add special PostgresType INET
Type::addType('inet', 'Doctrine\DBAL\Types\PostgresInetType');
$objDBALConnection->getDatabasePlatform()->registerDoctrineTypeMapping('Inet', 'inet');

// Instanciate EntityManager

$objDoctEntityManager = EntityManager::create($objDBALConnection, $objDoctConfig);

// ...

When running

php doctrine.php orm:validate-schema --verbose

I get the following:

[Mapping]  OK - The mapping files are correct.



  [Doctrine\DBAL\DBALException]                                                                         
  Unknown database type inet requested, Doctrine\DBAL\Platforms\PostgreSqlPlatform may not support it.  



Exception trace:
 () at /var/www/vhosts/workflow/project/Doctrine/DBAL/Platforms/AbstractPlatform.php:261
 Doctrine\DBAL\Platforms\AbstractPlatform->getDoctrineTypeMapping() at /var/www/vhosts/workflow/project/Doctrine/DBAL/Schema/PostgreSqlSchemaManager.php:285
 Doctrine\DBAL\Schema\PostgreSqlSchemaManager->_getPortableTableColumnDefinition() at /var/www/vhosts/workflow/project/Doctrine/DBAL/Schema/AbstractSchemaManager.php:672
 Doctrine\DBAL\Schema\AbstractSchemaManager->_getPortableTableColumnList() at /var/www/vhosts/workflow/project/Doctrine/DBAL/Schema/AbstractSchemaManager.php:159
 Doctrine\DBAL\Schema\AbstractSchemaManager->listTableColumns() at /var/www/vhosts/workflow/project/Doctrine/DBAL/Schema/AbstractSchemaManager.php:254
 Doctrine\DBAL\Schema\AbstractSchemaManager->listTableDetails() at /var/www/vhosts/workflow/project/Doctrine/DBAL/Schema/AbstractSchemaManager.php:242
 Doctrine\DBAL\Schema\AbstractSchemaManager->listTables() at /var/www/vhosts/workflow/project/Doctrine/DBAL/Schema/AbstractSchemaManager.php:830
 Doctrine\DBAL\Schema\AbstractSchemaManager->createSchema() at /var/www/vhosts/workflow/project/Doctrine/ORM/Tools/SchemaTool.php:689
 Doctrine\ORM\Tools\SchemaTool->getUpdateSchemaSql() at /var/www/vhosts/workflow/project/Doctrine/ORM/Tools/SchemaValidator.php:258
 Doctrine\ORM\Tools\SchemaValidator->schemaInSyncWithMetadata() at /var/www/vhosts/workflow/project/Doctrine/ORM/Tools/Console/Command/ValidateSchemaCommand.php:80
 Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand->execute() at /var/www/vhosts/workflow/project/Doctrine/Symfony/Component/Console/Command/Command.php:187
 Symfony\Component\Console\Command\Command->run() at /var/www/vhosts/workflow/project/Doctrine/Symfony/Component/Console/Application.php:194
 Symfony\Component\Console\Application->doRun() at /var/www/vhosts/workflow/project/Doctrine/Symfony/Component/Console/Application.php:118
 Symfony\Component\Console\Application->run() at /var/www/vhosts/workflow/project/Doctrine/ORM/Tools/Console/ConsoleRunner.php:39
 Doctrine\ORM\Tools\Console\ConsoleRunner::run() at /var/www/vhosts/workflow/project/doctrine.php:36


orm:validate-schema

I throw an eye to DBAL/Platforms/AbstractPlatform.php abstract class and DBAL/Platforms/PostgresSqlPlatform.php implementation. It appears initializeDoctrineTypeMappings() does not care about user custom types that might be declared.

@doctrinebot

Comment created by @beberlei:

Yes, you have to register them manually using AbstractPlatform#registerDoctrineTypeMapping.

@doctrinebot

Issue was closed with resolution "Invalid"

@doctrinebot doctrinebot added the Bug label
@beberlei beberlei was assigned by doctrinebot
@doctrinebot doctrinebot closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.