assoconnect/doctrine-types-bundle
Symfony bundle integrating Symfony Validator with Doctrine custom DBAL types to avoid duplicate validation/mapping code. Includes common value-object types (money/amount, IBAN/BIC, email, phone, country/currency, locale/timezone, IDs) with nullable and non-nullable support.
Install the Bundle (via Composer):
composer require assoconnect/doctrine-types-bundle
Note: Requires PHP 8.4+ and Symfony components (e.g., symfony/validator).
Register the Bundle (in config/app.php):
'extra' => [
'bundles' => [
Assoconnect\DoctrineTypesBundle\AssoconnectDoctrineTypesBundle::class => ['all' => true],
],
],
Configure Doctrine DBAL (in config/doctrine.php):
'dbal' => [
'types' => [
'amount' => Assoconnect\DoctrineTypesBundle\DBAL\Types\AmountType::class,
'spanish_nif' => Assoconnect\DoctrineTypesBundle\DBAL\Types\SpanishNifType::class,
// Add other types as needed
],
],
First Use Case: Spanish NIF Validation Update a migration to use the new type:
Schema::create('clients', function (Blueprint $table) {
$table->string('nif')->type('spanish_nif'); // Uses SpanishNifType
$table->timestamps();
});
Define Entity Properties Use Doctrine types in your model (via annotations or YAML/XML):
/**
* @ORM\Column(type="spanish_nif")
*/
private string $nif;
Leverage Built-in Validation The bundle auto-integrates with Symfony’s validator. For Laravel, manually map constraints:
use Illuminate\Validation\Rule;
$rules = [
'nif' => ['required', Rule::custom(function ($attribute, $value) {
return SpanishNifType::validate($value); // Reuse type logic
})],
];
Custom Type Extension Extend existing types (e.g., for locale-specific rules):
class CustomSpanishNifType extends SpanishNifType {
public function getSQLDeclaration(array $column, AbstractPlatform $platform) {
return 'VARCHAR(255) COMMENT \'Custom Spanish NIF\'';
}
}
Query Optimization Use native types in raw queries:
DB::select('SELECT * FROM clients WHERE nif = ?', ['12345678A']);
laravel-doctrine/orm to bridge Doctrine types to Eloquent.class SpanishNifRule extends Rule {
public function validate($attribute, $value, $fail) {
if (!SpanishNifType::validate($value)) {
$fail('Invalid Spanish NIF.');
}
}
}
type() over string() for type safety:
$table->string('iban')->type('iban'); // Uses IbanType
PHP Version Mismatch
composer.json.Validation System Conflict
Validator::extend() or custom rules.ORM vs. DBAL Confusion
laravel-doctrine/orm; DBAL-only usage may miss type mappings.config/doctrine.php under dbal.types.Type-Specific Edge Cases
SpanishNifType may not cover all business rules (e.g., historical formats).Type Registration Errors:
Check if the bundle is enabled in config/bundles.php and types are listed in config/doctrine.php.
php bin/console debug:container assoconnect_doctrine_types
Validation Failures:
Use SpanishNifType::validate() directly to debug:
var_dump(SpanishNifType::validate('12345678Z')); // Returns bool
Add Custom Types
Create a new type class (e.g., MyCustomType) and register it in config/doctrine.php:
'types' => [
'my_custom_type' => App\Doctrine\DBAL\Types\MyCustomType::class,
],
Override Default Behavior
Extend existing types (e.g., MoneyType) to modify SQL generation or validation:
class CustomMoneyType extends MoneyType {
public function convertToDatabaseValue($value, AbstractPlatform $platform) {
return round($value, 2); // Force 2 decimal places
}
}
Laravel-Specific Wrappers Create a facade to abstract Doctrine types:
class DoctrineTypeFacade {
public static function validateSpanishNif(string $nif): bool {
return SpanishNifType::validate($nif);
}
}
iban, nif).EXPLAIN ANALYZE to compare custom types vs. native VARCHAR.Type::validate() for repeated checks./**
* @ORM\Column(type="spanish_nif", nullable=true)
*/
private ?string $nif = null;
EmailType) are case-insensitive by default. Override if needed:
class CaseSensitiveEmailType extends EmailType {
public function convertToDatabaseValue($value, AbstractPlatform $platform) {
return strtoupper($value);
}
}
How can I help you explore Laravel packages today?