carbonphp/carbon-doctrine-types
Doctrine DBAL types to seamlessly persist and hydrate Carbon\Carbon and Carbon\CarbonImmutable instances. Adds custom date/time types for Carbon, improving timezone/precision handling and integration with Doctrine entities and migrations.
Start by installing the package via Composer: composer require carbonphp/carbon-doctrine-types. Next, register the DBAL types in your Doctrine configuration — typically in config/packages/doctrine.php (Symfony) or config/doctrine.php (plain DBAL). Use the Type::add() method to map carbon and/or carbon_immutable to their respective classes (Carbon\Doctrine\CarbonType and Carbon\Doctrine\CarbonImmutableType). Finally, update your entity properties to use Carbon/CarbonImmutable and mark them with @ORM\Column(type="carbon") (or carbon_immutable). Your first entity save/read operation should now work out of the box — verify with tinker or a quick test route.
Carbon for mutable temporal properties (e.g., lastLoginAt) and CarbonImmutable for stability in value objects (e.g., createdAt).datetime or timestamptz) to Carbon; rely on the library to handle DB-level casts (e.g., '2024-05-01 10:00:00' ↔ Carbon).boot() method (e.g., Type::add('carbon', CarbonType::class); Type::overrideType('datetime', CarbonType::class);).datetime/datetime_immutable columns with carbon/carbon_immutable gradually — the library handles backward compatibility with existing DB values as long as formats match.default_timezone and ensure Carbon::setFallbackLocale() is used in CLI/web contexts to prevent drift across environments.timestamptz. Use CarbonImmutable + setTimezone() only at presentation layer — avoid storing raw timezones unless denormalized.null; ensure your DB column allows NULL and type-hint properties accordingly (?Carbon, Carbon|null).carbon with Doctrine’s @CustomOverride — if overriding datetime, ensure no legacy fields assume DateTimeInterface.Carbon::setTestNow() in tests to assert predictable persistence/hydration without mocking DB time.Carbon::parse() in setters since hydration already handles it; prefer direct assignment.Carbon\Doctrine\CarbonType and override convertToPHPValue() — but prefer extending via config overrides over forks.How can I help you explore Laravel packages today?