Installation Add the package via Composer:
composer require c33s/doctrine-extra
Ensure doctrine/orm is also installed (required dependency).
First Use Case
Extend a Doctrine entity with a trait from c33s/doctrine-extra. For example, use Timestampable for automatic timestamp management:
use Doctrine\ORM\Mapping as ORM;
use C33S\DoctrineExtra\Traits\Timestampable;
#[ORM\Entity]
class Post
{
use Timestampable;
#[ORM\Id, ORM\GeneratedValue, ORM\Column]
private ?int $id = null;
}
src/Traits/ for available traits.src/DoctrineExtraBundle.php (if using Symfony) for bundle configuration.Timestamp Management
Use Timestampable to auto-set createdAt and updatedAt fields:
use C33S\DoctrineExtra\Traits\Timestampable;
class Post
{
use Timestampable;
#[ORM\Column(type: 'datetime')]
private ?\DateTimeInterface $createdAt = null;
#[ORM\Column(type: 'datetime')]
private ?\DateTimeInterface $updatedAt = null;
}
Sluggable Entities
Use Sluggable to generate SEO-friendly slugs:
use C33S\DoctrineExtra\Traits\Sluggable;
class Post
{
use Sluggable;
#[ORM\Column(length: 255, unique: true)]
private ?string $slug = null;
#[ORM\Column(length: 255)]
private string $title;
}
getSlugSource() and getSlugOptions() in your entity.Soft Deletes
Use SoftDeletable for soft-deletion logic:
use C33S\DoctrineExtra\Traits\SoftDeletable;
class Post
{
use SoftDeletable;
#[ORM\Column(type: 'datetime', nullable: true)]
private ?\DateTimeInterface $deletedAt = null;
}
SoftDeleteableFilter to your EntityManager in Symfony or manually in Laravel.Blameable Entities
Track entity creators/updaters with Blameable:
use C33S\DoctrineExtra\Traits\Blameable;
class Post
{
use Blameable;
#[ORM\ManyToOne(targetEntity: User::class)]
private ?User $createdBy = null;
#[ORM\ManyToOne(targetEntity: User::class)]
private ?User $updatedBy = null;
}
setUserProvider() to inject the current user (e.g., from Symfony’s Security component).Laravel-Specific:
doctrine/orm via spatie/laravel-doctrine-orm for seamless integration.getUser() in Blameable to fetch the authenticated user from Laravel’s Auth facade.SoftDeletes trait or use Doctrine’s SoftDeleteableFilter in queries.Symfony-Specific:
DoctrineExtraBundle in config/bundles.php.Blameable.Timestampable Defaults
createdAt/updatedAt are not nullable, ensure they are initialized in the constructor or Doctrine will throw errors.nullable: true in annotations.Sluggable Conflicts
getSlugSource() returns null or empty values.getSlugSource().Soft Deletes and Queries
SoftDeleteableFilter to the EntityManager will bypass soft-delete logic.doctrine.extra.listener.soft_deleteable service. In Laravel, manually apply the filter in repository methods:
$query->andWhere('p.deletedAt IS NULL');
Blameable User Provider
getUser() returns null, createdBy/updatedBy will be null.getUser().Doctrine Lifecycle Events
prePersist, preUpdate). Ensure your entities are properly mapped and events are not overridden.Enable Doctrine Logging:
$entityManager->getConnection()->getConfiguration()->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
Verify events are triggered (e.g., updatedAt updates).
Check Trait Methods:
Use var_dump() or dd() in overridden methods (e.g., setSlugSource()) to debug slug generation.
Query Builder Issues: For soft deletes, inspect the generated SQL to confirm filters are applied:
$query->getSQL(); // Check for `AND p.deleted_at IS NULL`
Custom Traits
Extend existing traits or create new ones by copying the pattern (e.g., Timestampable):
trait CustomTrait
{
public function prePersist()
{
// Custom logic
}
}
Override Trait Methods Customize behavior by overriding trait methods:
class Post
{
use Sluggable;
public function getSlugSource(): string
{
return $this->title . '-' . $this->id; // Custom slug logic
}
}
Event Subscribers Use Doctrine’s event system to extend functionality:
$eventManager->addEventSubscriber(new class implements EventSubscriber {
public function getSubscribedEvents(): array
{
return [
Events::prePersist,
Events::preUpdate,
];
}
// Custom logic
});
Configuration
config/packages/doctrine_extra.yaml.How can I help you explore Laravel packages today?