Installation via Composer (if using Satis):
composer require awaresoft/doctrine-bundle
Note: Due to symlinking requirements, follow the README’s local setup guide if modifying the bundle.
Enable the Bundle in config/bundles.php:
return [
// ...
Awaresoft\DoctrineBundle\AwaresoftDoctrineBundle::class => ['all' => true],
];
First Use Case:
prePersist, postLoad) without writing repetitive listeners.EntityListener in your entity:
use Awaresoft\DoctrineBundle\Event\LifecycleEvent;
use Doctrine\ORM\Mapping as ORM;
#[ORM\Entity]
class User
{
#[LifecycleEvent('prePersist')]
public function setDefaultRole()
{
$this->role = 'user'; // Custom logic before save
}
}
# config/packages/awaresoft_doctrine.yaml
awaresoft_doctrine:
listeners:
App\Entity\User:
prePersist: ['setDefaultRole']
postLoad: ['logEntityLoad']
use Awaresoft\DoctrineBundle\Repository\AbstractRepository;
class UserRepository extends AbstractRepository
{
public function findActiveUsers()
{
return $this->createQueryBuilder('u')
->where('u.isActive = :active')
->setParameter('active', true)
->getQuery()
->getResult();
}
}
findByStatus) across projects.use Awaresoft\DoctrineBundle\Query\QueryHelper;
$qb = $entityManager->getRepository(User::class)->createQueryBuilder('u');
$qb->andWhere(QueryHelper::isActive('u'));
// src/EventListener/UserListener.php
use Awaresoft\DoctrineBundle\Event\LifecycleEventArgs;
class UserListener
{
public function onPrePersist(LifecycleEventArgs $args)
{
$user = $args->getEntity();
$user->setLastLogin(new \DateTime());
}
}
services.yaml:
services:
App\EventListener\UserListener:
tags:
- { name: 'awaresoft_doctrine.listener', event: 'prePersist', entity: 'App\Entity\User' }
Symlinking Issues:
/src/Awaresoft is symlinked correctly. Clear cache after changes:
php bin/console cache:clear
composer.json for missing autoload_psr4 entries.Event Priority Conflicts:
awaresoft_doctrine.yaml:
listeners:
App\Entity\User:
prePersist:
- ['setDefaultRole', 10] # Lower number = higher priority
- ['logCreation', 20]
FakerBundle Dependency:
willdurand/faker-bundle. Install it if using test data generation:
composer require willdurand/faker-bundle
# config/packages/dev/doctrine.yaml
doctrine:
dbal:
logging: true
profiling: true
/profiler) to verify events trigger as expected.Custom Event Subscribers:
Awaresoft\DoctrineBundle\EventSubscriber\EventSubscriberInterface for cross-cutting concerns (e.g., logging, caching).Query DSL:
QueryHelper to add project-specific conditions:
namespace App\Doctrine\Query;
use Awaresoft\DoctrineBundle\Query\QueryHelper as BaseQueryHelper;
class QueryHelper extends BaseQueryHelper
{
public static function isVerified($alias)
{
return "$alias.verified = true";
}
}
Configuration Overrides:
config/packages/awaresoft_doctrine.yaml:
awaresoft_doctrine:
default_listener_priority: 0 # Adjust globally
query_helpers:
- App\Doctrine\Query\QueryHelper
Awaresoft\DoctrineBundle\Batch\BatchProcessor for bulk operations:
$processor = new BatchProcessor($entityManager, User::class);
$processor->process($users, 50); // 50 entities per batch
QueryHelper::join() for eager-loading:
$qb->addSelect('u.profile')
->leftJoin('u.profile', 'p')
->where(QueryHelper::join('u', 'p', 'p.isActive = true'));
How can I help you explore Laravel packages today?