Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Entity Helper Bundle Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. 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(),
    
  2. 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
        }
    }
    
  3. Key Classes:

    • EntityHelper: Core helper for CRUD operations.
    • EntityManager: Wrapped Doctrine ORM methods (e.g., find(), findOneBy()).
    • Where to look first: README and src/Helper/ for available methods.

Implementation Patterns

Common Workflows

  1. 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);
    
  2. Bulk Operations:

    $entities = $entityHelper->findAll(User::class);
    $entityHelper->removeAll($entities); // Bulk delete
    
  3. 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);
    
  4. 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
        }
    );
    
  5. 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();
    

Integration Tips

  • Symfony Forms: Use the helper to populate form data:
    $user = $entityHelper->findOneBy(['id' => $id], User::class);
    $form = $this->createForm(UserType::class, $user);
    
  • API Responses: Serialize entities with helper methods:
    $data = $entityHelper->toArray($entity); // Hypothetical method (check docs)
    return $this->json($data);
    
  • Testing: Mock EntityHelper in unit tests:
    $helper = $this->createMock(EntityHelper::class);
    $helper->method('findOneBy')->willReturn($mockEntity);
    

Gotchas and Tips

Pitfalls

  1. Doctrine Version Compatibility:

    • The bundle may not support the latest Doctrine ORM. Verify compatibility in the changelog or test with your version.
    • Workaround: Extend the helper or wrap calls in a try-catch block.
  2. EntityManager Access:

    • The helper provides a wrapped EntityManager, but some advanced Doctrine features (e.g., Connection, EventManager) may require direct access.
    • Tip: Use $entityHelper->getEntityManager() to access underlying methods.
  3. Performance with Large Datasets:

    • Methods like findAll() can be memory-intensive. Use pagination or chunking:
      $entities = $entityHelper->findBy([], User::class, ['id' => 'ASC'], 10, 0);
      
  4. Missing Documentation:

    • The bundle lacks detailed method documentation. Inspect the source for available methods.
    • Tip: Use ReflectionClass to introspect methods:
      $reflection = new ReflectionClass(EntityHelper::class);
      $methods = $reflection->getMethods();
      
  5. Transaction Handling:

    • The helper does not manage transactions by default. Use Symfony’s 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;
      }
      

Debugging

  1. Enable Doctrine Logging:

    # config/packages/doctrine.yaml
    doctrine:
        dbal:
            logging: true
            profiling: true
    
    • Check logs for SQL queries or errors.
  2. Check for Deprecated Methods:

    • The bundle may use deprecated Doctrine methods. Update calls if warnings appear.
  3. Entity Not Found:

    • If findOneBy() returns null, verify:
      • The entity class is correct.
      • Criteria match existing data.
      • The entity is managed by Doctrine (e.g., not a DTO).

Extension Points

  1. 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']
    
  2. Override EntityManager: Replace the default EntityManager with a custom implementation:

    services:
        linkin_entity_helper.helper.entity:
            arguments:
                - '@custom.doctrine.entity_manager'
    
  3. Add Query Filters: Use Doctrine’s Filter system via the helper:

    $filter = $entityHelper->getEntityManager()->getFilters()->enable('softdelete');
    
  4. Event Subscribers: Attach subscribers to the helper’s EntityManager:

    $entityHelper->getEntityManager()->getEventManager()->addEventSubscriber(
        new YourSubscriber()
    );
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
terminal42/code-quality-tools
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky