drjele/doctrine-type
Laravel package to add custom Doctrine DBAL types and mappings, improving schema introspection and migrations for non-standard database column types. Helps register and use bespoke types cleanly across your application.
Installation Add the package via Composer:
composer require drjele/doctrine-type
Publish the config (if needed):
php artisan vendor:publish --provider="Drjele\DoctrineType\DoctrineTypeServiceProvider"
Basic Usage Register a custom Doctrine type in your Laravel app by extending the base type system:
use Drjele\DoctrineType\DoctrineType;
use Doctrine\DBAL\Types\Type;
// Define a new type (e.g., for UUIDs, JSON, or custom formats)
Type::addType('custom_type', DoctrineType::class);
First Use Case Use the type in a Laravel model via Doctrine DBAL (if integrating with Eloquent via a custom accessor/mutator or a hybrid ORM):
use Doctrine\DBAL\Types\Type;
// Example: Store a JSON string as a custom type
$type = Type::getType('custom_type');
$value = $type->convertToDatabaseValue(['key' => 'value'], $platform);
Custom Type Registration
Extend DoctrineType to handle serialization/deserialization:
class JsonType extends DoctrineType {
public function convertToDatabaseValue($value, Platform $platform) {
return json_encode($value);
}
public function convertToPHPValue($value, Platform $platform) {
return json_decode($value, true);
}
}
Type::addType('json', JsonType::class);
Integration with Eloquent Use a model accessor/mutator or a custom attribute cast to bridge Doctrine types with Eloquent:
// In a model
protected $casts = [
'metadata' => 'array', // Cast to array (handled by Doctrine type)
];
// Or via a custom attribute
public function getMetadataAttribute($value) {
$type = Type::getType('json');
return $type->convertToPHPValue($value, $this->getConnection()->getDoctrineSchemaManager()->getDatabasePlatform());
}
Query Builder Integration
Use raw Doctrine queries via Laravel’s DB::connection()->getDoctrineConnection():
$doctrineConnection = DB::connection()->getDoctrineConnection();
$query = $doctrineConnection->createQueryBuilder()
->select('u')
->from('users', 'u')
->where('u.metadata->>\'$.key\' = :value')
->setParameter('value', 'target')
->getQuery();
Platform-Specific Quirks
convertToDatabaseValue/convertToPHPValue across platforms.Caching Issues
php artisan doctrine:cache:clear
Eloquent Integration Gaps
Type Registration Order
boot() method).$doctrineConnection->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
Custom Platforms
Override getSQLDeclaration for platform-specific schema definitions:
public function getSQLDeclaration(array $column, Platform $platform) {
return match ($platform->getName()) {
'postgresql' => 'JSONB',
default => 'TEXT',
};
}
Validation
Add validation logic in convertToDatabaseValue:
if (!is_array($value)) {
throw new \InvalidArgumentException('Value must be an array.');
}
Doctrine Events
Listen to onFlush or postLoad for lifecycle hooks:
$eventManager->addEventListener(
\Doctrine\ORM\Events::postLoad,
function ($event) {
// Transform loaded data
}
);
How can I help you explore Laravel packages today?