Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Connection can accept object as well as classname for new doctrine type #44149

Merged

Conversation

emiliopedrollo
Copy link
Contributor

@emiliopedrollo emiliopedrollo commented Sep 15, 2022

We use custom types on postgresql to create our enums there. That means that we have to create classes for each one of them:

class CustomType1 extends Type
{
  public function getSQLDeclaration(array $column, AbstractPlatform $platform)
  {
      return 'custom_type1';
  }
  
  public function getName()
  {
      return 'custom_type1';
  }
}

And then register them:

Connection::resolverFor('pgsql', function($connection, $database, $prefix, $config) {
  $connection = new PostgresConnection($connection, $database, $prefix, $config);
  
  $connection->registerDoctrineType(CustomType1::class, 'custom_type1', 'string')
  $connection->registerDoctrineType(CustomType2::class, 'custom_type2', 'string')
  $connection->registerDoctrineType(CustomType3::class, 'custom_type3', 'string')
  // ...
  
  return $connection;
}

We have a growing number of enums (30+ so far) and this has become cumbersome.

So, instead of creating a huge number of classes if we could pass a Type object to registerDoctrineType we could just implement something like:

Connection::resolverFor('pgsql', function($connection, $database, $prefix, $config) {
  $connection = new PostgresConnection($connection, $database, $prefix, $config);
  
  foreach (self::$enums as $type) {
    $connection->registerDoctrineType((new class extends Type {
      protected $name;

      public function getSQLDeclaration(array $column, AbstractPlatform $platform)
      {
        return $this->name;
      }

      public function getName()
      {
        return $this->name;
      }

      public function setName($name): self
      {
        $this->name = $name;
        return $this;
      }
    })->setName($type), $type, 'string');
  }
    
  return $connection;
}

I know it's a niche thing but I can't see a downside to this implementation

@emiliopedrollo emiliopedrollo changed the title Connection can accept object as well as classname [9.x] Connection can accept object as well as classname Sep 15, 2022
@emiliopedrollo emiliopedrollo changed the title [9.x] Connection can accept object as well as classname [9.x] Connection can accept object as well as classname for new doctrine type Sep 15, 2022
@taylorotwell taylorotwell merged commit b088269 into laravel:9.x Sep 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants