tourze / symfony-schedule-entity-clean-bundle
Installs: 4
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:symfony-bundle
Requires
- php: >=8.1
- doctrine/doctrine-bundle: ^2.13
- dragonmantank/cron-expression: ^3.4
- nesbot/carbon: ^2.72 || ^3
- symfony/event-dispatcher: ^6.4
- symfony/framework-bundle: ^6.4
- tourze/symfony-cron-job-bundle: 0.0.*
This package is auto-updated.
Last update: 2025-03-31 00:31:17 UTC
README
A Symfony bundle for automatically cleaning old entity data based on scheduled cron expressions.
Features
- Automatically clean old entity data based on cron expressions
- Configurable retention period for each entity
- Custom retention period via environment variables
- Asynchronous processing using Symfony Messenger
- Event dispatching after cleaning operations
Installation
composer require tourze/symfony-schedule-entity-clean-bundle
Register the bundle in your bundles.php
:
return [ // ... Tourze\ScheduleEntityCleanBundle\ScheduleEntityCleanBundle::class => ['all' => true], // ... ];
Quick Start
- Mark your entity class with the
AsScheduleClean
attribute:
<?php namespace App\Entity; use Doctrine\ORM\Mapping as ORM; use Tourze\ScheduleEntityCleanBundle\Attribute\AsScheduleClean; #[ORM\Entity] #[AsScheduleClean(expression: '0 0 * * *', defaultKeepDay: 30)] class LogEntry { #[ORM\Id] #[ORM\GeneratedValue] #[ORM\Column] private ?int $id = null; #[ORM\Column] private \DateTimeImmutable $createTime; // Other properties and methods... }
-
Ensure your entity has a
createTime
field. The bundle uses this field to determine which records to delete. -
The bundle will automatically register a cron job that runs every minute to check if any entities need cleaning.
Configuration
The AsScheduleClean
attribute accepts the following parameters:
expression
: A cron expression that determines when to clean the entity (default: '0 0 ** *', which runs at midnight every day)defaultKeepDay
: The number of days to keep records (default: 7)keepDayEnv
: An optional environment variable name that can override the defaultKeepDay value
Events
The bundle dispatches a ScheduleEntityCleanFinishEvent
after successfully cleaning entity data. You can listen to this event to perform custom actions after the cleaning process.
<?php namespace App\EventListener; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Tourze\ScheduleEntityCleanBundle\Event\ScheduleEntityCleanFinishEvent; class EntityCleanSubscriber implements EventSubscriberInterface { public static function getSubscribedEvents(): array { return [ ScheduleEntityCleanFinishEvent::class => 'onEntityCleaned', ]; } public function onEntityCleaned(ScheduleEntityCleanFinishEvent $event): void { $modelClass = $event->getModelClass(); // Do something with the model class } }
Requirements
- PHP 8.1 or higher
- Symfony 6.4 or higher
- Doctrine ORM
- Entity must have a
createTime
field
License
The MIT License (MIT). Please see License File for more information.