Doctrine DBAL
  1. Doctrine DBAL
  2. DBAL-230

Custom types not taken into account when running $ doctrine orm:validate-schema

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Invalid
    • Affects Version/s: 2.2
    • Fix Version/s: None
    • Component/s: Drivers
    • Labels:
      None
    • Environment:
      Postgres

      Description

      Hi,

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

      PostgresInetType.php
      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;
          }
      
      }
      
      bootstrap.php
      // ... 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.

        Activity

        Hide
        Benjamin Eberlei added a comment -

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

        Show
        Benjamin Eberlei added a comment - Yes, you have to register them manually using AbstractPlatform#registerDoctrineTypeMapping.

          People

          • Assignee:
            Benjamin Eberlei
            Reporter:
            Maxime MARAIS
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: