adrenalinkin/entity-helper-bundle
Symfony bundle providing a Doctrine entity helper service to streamline common entity operations. Easily create managed entity instances by class name and access helper methods via the service container, reducing boilerplate when working with Doctrine entities.
Installation:
composer require adrenalinkin/entity-helper-bundle
Add the bundle to AppKernel.php (Symfony 3.x) or config/bundles.php (Symfony 4.x+):
new Linkin\Bundle\EntityHelperBundle\LinkinEntityHelperBundle(),
First Use Case: Retrieve the helper via dependency injection:
use Linkin\Bundle\EntityHelperBundle\Helper\EntityHelper;
class MyController
{
public function __construct(private EntityHelper $entityHelper) {}
public function index()
{
$entity = $this->entityHelper->findOneBy(['id' => 1], MyEntity::class);
// Or use other helper methods
}
}
Key Classes:
EntityHelper: Core helper for CRUD operations.EntityManager: Wrapped Doctrine ORM methods (e.g., find(), findOneBy()).src/Helper/ for available methods.Basic CRUD Operations:
// Find entities
$entities = $entityHelper->findBy(['status' => 'active'], User::class);
$entity = $entityHelper->findOneBy(['email' => 'test@example.com'], User::class);
// Create/update/delete
$entityHelper->persist($entity);
$entityHelper->flush();
$entityHelper->remove($entity);
Bulk Operations:
$entities = $entityHelper->findAll(User::class);
$entityHelper->removeAll($entities); // Bulk delete
Repository Abstraction: Replace repetitive repository calls with helper methods:
// Instead of:
$repo = $em->getRepository(User::class);
$user = $repo->findOneBy(['email' => 'test@example.com']);
// Use:
$user = $entityHelper->findOneBy(['email' => 'test@example.com'], User::class);
Event Integration:
Use the helper in Doctrine lifecycle events (e.g., prePersist, preUpdate):
$entityHelper->getEntityManager()->getEventManager()->addEventListener(
'prePersist',
function (LifecycleEventArgs $args) {
$entity = $args->getEntity();
// Modify entity before persist
}
);
Custom Query Handling: For complex queries, combine with Doctrine QueryBuilder:
$qb = $entityHelper->createQueryBuilder(User::class);
$qb->where('u.status = :status')->setParameter('status', 'active');
$users = $qb->getQuery()->getResult();
$user = $entityHelper->findOneBy(['id' => $id], User::class);
$form = $this->createForm(UserType::class, $user);
$data = $entityHelper->toArray($entity); // Hypothetical method (check docs)
return $this->json($data);
EntityHelper in unit tests:
$helper = $this->createMock(EntityHelper::class);
$helper->method('findOneBy')->willReturn($mockEntity);
Doctrine Version Compatibility:
EntityManager Access:
EntityManager, but some advanced Doctrine features (e.g., Connection, EventManager) may require direct access.$entityHelper->getEntityManager() to access underlying methods.Performance with Large Datasets:
findAll() can be memory-intensive. Use pagination or chunking:
$entities = $entityHelper->findBy([], User::class, ['id' => 'ASC'], 10, 0);
Missing Documentation:
ReflectionClass to introspect methods:
$reflection = new ReflectionClass(EntityHelper::class);
$methods = $reflection->getMethods();
Transaction Handling:
Transaction component or Doctrine’s Transaction:
$entityHelper->getEntityManager()->beginTransaction();
try {
$entityHelper->persist($entity);
$entityHelper->flush();
$entityHelper->getEntityManager()->commit();
} catch (\Exception $e) {
$entityHelper->getEntityManager()->rollback();
throw $e;
}
Enable Doctrine Logging:
# config/packages/doctrine.yaml
doctrine:
dbal:
logging: true
profiling: true
Check for Deprecated Methods:
Entity Not Found:
findOneBy() returns null, verify:
Custom Helper Methods: Extend the base helper to add domain-specific methods:
use Linkin\Bundle\EntityHelperBundle\Helper\EntityHelper;
class CustomEntityHelper extends EntityHelper
{
public function findActiveUsers()
{
return $this->findBy(['status' => 'active'], User::class);
}
}
Register the service in services.yaml:
services:
App\Helper\CustomEntityHelper:
parent: linkin_entity_helper.helper.entity
arguments: ['@doctrine.orm.entity_manager']
Override EntityManager:
Replace the default EntityManager with a custom implementation:
services:
linkin_entity_helper.helper.entity:
arguments:
- '@custom.doctrine.entity_manager'
Add Query Filters:
Use Doctrine’s Filter system via the helper:
$filter = $entityHelper->getEntityManager()->getFilters()->enable('softdelete');
Event Subscribers:
Attach subscribers to the helper’s EntityManager:
$entityHelper->getEntityManager()->getEventManager()->addEventSubscriber(
new YourSubscriber()
);
How can I help you explore Laravel packages today?