desarrolla2/doctrine-mirror-bundle
Installation Add the package via Composer (if available in a private repo or fork):
composer require desarrolla2/doctrine-mirror-bundle
Note: Since the last release was in 2017, verify compatibility with your Laravel/Doctrine versions (e.g., Doctrine ORM 2.x).
Bundle Registration
In config/app.php, add the bundle to the extra.bundles array under framework:
'framework' => [
'bundles' => [
// ...
Desarrolla2\DoctrineMirrorBundle\Desarrolla2DoctrineMirrorBundle::class,
],
],
Basic Configuration
Check config/packages/desarrolla2_doctrine_mirror.yaml (if auto-generated) or define a custom config:
desarrolla2_doctrine_mirror:
enabled: true
mirror_entity: App\Entity\MirrorEntity # Your custom entity to mirror data
sync_interval: 3600 # Sync interval in seconds (e.g., hourly)
First Use Case: Mirroring Data Define a Doctrine entity annotated for mirroring (example):
// src/Entity/MirrorEntity.php
use Desarrolla2\DoctrineMirrorBundle\Annotation\Mirror;
/**
* @Mirror(table="original_table", fields={"id", "name", "created_at"})
*/
class MirrorEntity {}
Run a manual sync via CLI (if supported):
php bin/console desarrolla2:doctrine-mirror:sync
Entity Mirroring
@Mirror annotation to define which tables/fields to mirror.User table to a UserArchive table with selective fields./**
* @Mirror(
* table="users",
* fields={"id", "email", "status"},
* where="deleted_at IS NULL"
* )
*/
class UserArchive {}
Event-Driven Sync
postPersist, postUpdate) to trigger mirror updates:// src/EventListener/MirrorSyncListener.php
use Desarrolla2\DoctrineMirrorBundle\Event\MirrorSyncEvent;
class MirrorSyncListener
{
public function onMirrorSync(MirrorSyncEvent $event)
{
// Custom logic (e.g., log, validate, or transform data)
}
}
Register the listener in services.yaml:
servicios:
Desarrolla2\DoctrineMirrorBundle\EventListener\MirrorSyncListener:
tags:
- { name: doctrine.event_listener, event: postPersist }
Scheduled Syncs
app/Console/Kernel.php) to run periodic syncs:protected function schedule(Schedule $schedule)
{
$schedule->command('desarrolla2:doctrine-mirror:sync')->hourly();
}
Bulk Operations
$em = $this->getEntityManager();
$query = $em->createQuery('SELECT u FROM App\Entity\User u WHERE u.status = :status')
->setParameter('status', 'active');
$query->setMaxResults(100); // Process in batches
Doctrine Version Mismatch
Annotation Parsing Issues
config/packages/doctrine.yaml:
doctrine:
orm:
mappings:
Desarrolla2DoctrineMirrorBundle:
type: annotation
dir: "%kernel.project_dir%/vendor/desarrolla2/doctrine-mirror-bundle/Resources/config/doctrine"
prefix: 'Desarrolla2\DoctrineMirrorBundle\Entity'
alias: Desarrolla2DoctrineMirrorBundle
Missing CLI Commands
src/Command/ for available commands or implement custom ones:
php bin/console list
Performance with Large Tables
>1M rows) may cause timeouts. Optimize with:
Enable SQL Logging
Add to config/packages/doctrine.yaml:
doctrine:
dbal:
logging: true
profiling: true
Check logs in var/log/doctrine.log.
Verify Entity Metadata Dump Doctrine metadata to debug mirroring setup:
$metadata = $em->getMetadataFactory()->getAllMetadata();
print_r($metadata);
Check Event Dispatching Ensure events are fired by adding a debug listener:
public function onKernelRequest(GetResponseEvent $event)
{
\Log::info('Mirror events enabled:', ['events' => $event->getRequest()->attributes->get('_doctrine')]);
}
Custom Mirror Strategies Override the default mirroring logic by extending the bundle’s services:
# config/services.yaml
servicios:
Desarrolla2\DoctrineMirrorBundle\Service\MirrorService:
class: App\Service\CustomMirrorService
arguments:
- '@doctrine.orm.entity_manager'
Add Pre/Post Sync Hooks
Extend the MirrorSyncEvent to inject custom logic:
class CustomMirrorSyncListener
{
public function onPreSync(MirrorSyncEvent $event)
{
// Pre-sync validation/transformation
}
public function onPostSync(MirrorSyncEvent $event)
{
// Post-sync actions (e.g., send notifications)
}
}
Support for Soft Deletes
If using Gedmo\SoftDeleteable, exclude deleted records in the @Mirror annotation:
/**
* @Mirror(where="deletedAt IS NULL")
*/
How can I help you explore Laravel packages today?