antishov/doctrine-extensions-bundle
Installation:
composer require antishov/doctrine-extensions-bundle
Enable the bundle in config/bundles.php:
Stof\DoctrineExtensionsBundle\StofDoctrineExtensionsBundle::class => ['all' => true],
First Use Case:
Use the Timestampable behavior to auto-track createdAt and updatedAt fields in an entity:
use Gedmo\Mapping\Annotation as Gedmo;
#[ORM\Entity]
class Post
{
#[ORM\Column(type: 'datetime')]
#[Gedmo\Timestampable(on: 'create')]
private $createdAt;
#[ORM\Column(type: 'datetime')]
#[Gedmo\Timestampable(on: 'update')]
private $updatedAt;
}
Run php bin/console doctrine:schema:update --force to apply changes.
gedmo/doctrine-extensions (v2.3.4+ or v3.0+): Core behaviors (e.g., Slugable, Loggable).Entity Behaviors:
#[Gedmo\Slug(fields: ['title'])]
private $slug;
#[Gedmo\Loggable]
class Product { ... }
#[Gedmo\Uploadable(filePath: 'uploads/products')]
private $imagePath;
Tree Structures:
Use NestedSet for hierarchical data (e.g., categories):
#[Gedmo\Tree(type: 'nested')]
class Category { ... }
Soft Deletes:
#[Gedmo\SoftDeleteable(fieldName: 'deletedAt')]
class User { ... }
Query soft-deleted records:
$queryBuilder->andWhere('u.deletedAt IS NULL');
Translatable:
#[Gedmo\Translatable]
class Article { ... }
Requires gedmo/doctrine-extensions:translatable and a translation manager.
prePersist/preUpdate for custom logic:
#[ORM\PrePersist]
public function setDefaultValues(): void {
$this->createdAt = new \DateTime();
}
Doctrine\ORM\EntityRepository for behavior-specific queries.Assert constraints with Uploadable:
#[Assert\File(maxSize: '1024k')]
private $file;
Uploadable Paths:
/), not backslashes (\), to avoid crashes.mkdir -p uploads/products && chmod -R 775 uploads
Soft Deletes + Loggable:
#[Gedmo\Loggable(logEntryClass: 'App\Entity\LogEntry')]
#[Gedmo\SoftDeleteable(fieldName: 'deletedAt')]
class Post { ... }
Slug Conflicts:
-2):
#[Gedmo\Slug(options: ['unique' => true, 'separator' => '-'])]
private $slug;
NestedSet Performance:
materializedPath for large trees.Doctrine Events: Enable debug logging for gedmo events:
# config/packages/dev/doctrine.yaml
doctrine:
orm:
eventmanager: 'doctrine.eventmanager'
Log events in a custom subscriber:
public function onPrePersist(LifecycleEventArgs $args): void {
\Log::debug('PrePersist: ' . $args->getObject()->getClassName());
}
Uploadable Errors:
UploadableManager for validation failures:
$manager = $this->container->get('stof_doctrine_extensions.uploadable.manager');
$manager->validate($entity);
Custom Behaviors:
Extend Gedmo\Mapping\Driver\AnnotationDriver to add annotations:
#[Gedmo\CustomBehavior('app_custom_behavior')]
class Entity { ... }
Event Subscribers:
Override default behavior (e.g., modify Loggable entries):
#[AsEventSubscriber]
class CustomLogSubscriber implements EventSubscriber {
public function getSubscribedEvents(): array {
return [LifecycleEvents::prePersist];
}
public function prePersist(LifecycleEventArgs $args): void { ... }
}
Configuration:
Override default settings in config/packages/stof_doctrine_extensions.yaml:
stof_doctrine_extensions:
uploadable:
default_upload_dir: '%kernel.project_dir%/public/uploads'
allowed_mime_types: ['image/jpeg', 'image/png']
How can I help you explore Laravel packages today?