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 Manager Generator Bundle Laravel Package

activpik/entity-manager-generator-bundle

Symfony2 bundle that generates basic Doctrine Entity Manager classes for your entities and registers them as services (services.xml). Adds save() and getRepository() helpers. Use doctrine:generate:entitymanager Bundle:Entity, then fetch via service id.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Steps

  1. Installation Add the bundle to composer.json:

    "require": {
        "activpik/entity-manager-generator-bundle": "dev-master"
    }
    

    Run:

    composer update
    
  2. Register the Bundle Add to app/AppKernel.php:

    new Activpik\EntityManagerGeneratorBundle\ActivpikEntityManagerGeneratorBundle(),
    
  3. Generate Your First Manager Run the console command for your entity:

    php app/console doctrine:generate:entitymanager BundleName:Entity
    

    Example:

    php app/console doctrine:generate:entitymanager ActivpikManagerBundle:Video
    

    This creates a VideoManager class in src/Activpik/ManagerBundle/Entity/.


First Use Case

Use Case: CRUD Operations with a Dedicated Manager

  1. Generate a Manager

    php app/console doctrine:generate:entitymanager AcmeDemoBundle:Product
    

    This creates ProductManager.php in src/Acme/DemoBundle/Entity/.

  2. Inject and Use in a Controller

    use Acme\DemoBundle\Entity\ProductManager;
    
    class ProductController extends Controller
    {
        public function indexAction()
        {
            $manager = $this->get('acme_demo.product_manager');
            $products = $manager->findAll(); // Assuming you extend the manager
            // ...
        }
    }
    

Implementation Patterns

Workflows

  1. Manager Generation Workflow

    • Generate a manager for each entity requiring custom logic.
    • Place generated managers in src/BundleName/Entity/ for consistency.
    • Extend the generated manager to add custom methods:
      namespace Acme\DemoBundle\Entity;
      
      use Doctrine\ORM\EntityManager;
      
      class ProductManager extends \Activpik\EntityManagerGeneratorBundle\GeneratedManager
      {
          public function getPublishedProducts()
          {
              return $this->em->getRepository('AcmeDemoBundle:Product')
                  ->findBy(['published' => true]);
          }
      }
      
  2. Service Integration

    • Register custom managers as services in services.xml (required by the bundle):
      <services>
          <service id="acme_demo.product_manager" class="Acme\DemoBundle\Entity\ProductManager">
              <argument type="service" id="doctrine.orm.entity_manager" />
              <tag name="doctrine.manager" entity="AcmeDemoBundle:Product" />
          </service>
      </services>
      
    • Access via dependency injection:
      $manager = $this->get('acme_demo.product_manager');
      
  3. Repository Access

    • Use the generated getRepository() method to interact with Doctrine repositories:
      $repository = $manager->getRepository();
      $product = $repository->find(1);
      

Integration Tips

  1. Symfony Forms Integrate managers with Symfony forms for validation and data handling:

    $form = $this->createFormBuilder($manager->createNew())
        ->add('name', TextType::class)
        ->getForm();
    
  2. Event Subscribers Attach event subscribers to managers for pre/post operations:

    $manager->getEntityManager()->getEventManager()->addEventSubscriber(new ProductSubscriber());
    
  3. Testing Mock managers in tests using PHPUnit:

    $mockManager = $this->createMock(ProductManager::class);
    $mockManager->method('findAll')->willReturn([$productMock]);
    $this->container->set('acme_demo.product_manager', $mockManager);
    

Gotchas and Tips

Pitfalls

  1. Services.xml Requirement

    • The bundle only works with bundles using services.xml. If your bundle uses autowiring or YAML, this bundle will fail silently or throw errors.
    • Fix: Migrate to services.xml or avoid using this bundle.
  2. Generated Manager Overwriting

    • Regenerating a manager will overwrite the existing file. Backup or version-control your custom logic.
    • Tip: Extend the generated manager instead of modifying it directly.
  3. Namespace Conflicts

    • The generated manager uses the same namespace as the entity. If your entity is in a subdirectory (e.g., src/Bundle/Entity/Model/Product), the manager will be generated in src/Bundle/Entity/Model/, which may not align with your project structure.
    • Tip: Manually move the generated file to a Manager directory if needed.
  4. Doctrine Lifecycle Events

    • The generated manager does not include lifecycle callbacks (e.g., prePersist). Add these manually:
      $manager->getEntityManager()->getEventManager()->addEventListener(
          [Product::class, 'ProductManager'],
          new ProductLifecycleListener()
      );
      

Debugging

  1. Command Not Found

    • Ensure the bundle is registered in AppKernel.php and composer.json is updated.
    • Debug: Run php app/console list to verify the command exists.
  2. Manager Not Autowired

    • If using autowiring, manually define the service in services.xml as shown above.
    • Debug: Check php app/console debug:container for the service ID.
  3. Repository Not Found

    • Verify the entity FQCN matches the repository path. Example:
      // Correct:
      $repository = $manager->getRepository('AcmeDemoBundle:Product');
      // Incorrect (if entity is in a sub-namespace):
      $repository = $manager->getRepository('Acme\DemoBundle\Entity\Model\Product');
      
    • Fix: Use the full bundle:entity format or adjust the repository path.

Extension Points

  1. Custom Manager Templates

    • Override the default manager template by extending the bundle or copying the template from vendor/activpik/entity-manager-generator-bundle/Resources/views/.
    • Example: Create a ProductManager.php.twig in your bundle’s Resources/views/ directory.
  2. Adding Common Methods

    • Extend the base generated manager to include reusable methods:
      namespace Acme\DemoBundle\Entity;
      
      use Activpik\EntityManagerGeneratorBundle\GeneratedManager;
      
      abstract class BaseManager extends GeneratedManager
      {
          public function findById($id)
          {
              return $this->em->find($this->getEntityClass(), $id);
          }
      }
      
    • Have your generated managers extend BaseManager.
  3. Dynamic Manager Generation

    • Use the bundle’s ManagerGenerator service programmatically to generate managers dynamically:
      $generator = $this->get('activpik_entity_manager_generator.manager_generator');
      $generator->generate('AcmeDemoBundle:Product');
      

Configuration Quirks

  1. Bundle Prefix in Service IDs

    • Service IDs follow the pattern bundle_prefix.manager.entity_name. Example:
      • For AcmeDemoBundle:Product, the service ID is acme_demo.product_manager.
    • Tip: Use php app/console debug:container to confirm the correct service ID.
  2. Entity Manager Injection

    • The generated manager expects the doctrine.orm.entity_manager service as its first argument. If you’re using a custom entity manager, inject it explicitly:
      <service id="acme_demo.product_manager" class="Acme\DemoBundle\Entity\ProductManager">
          <argument type="service" id="custom.entity_manager" />
      </service>
      
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.
make-dev/orca
dmstr/symfony-system-resources-bundle
dmstr/symfony-job-queue-bundle
dmstr/openapi-json-schema-bundle
dmstr/keycloak-security-bundle
dmstr/doctrine-audit-log-bundle
dmstr/api-platform-utils-bundle
dmstr/api-configuration-bundle
chrisdev/ux-components
baks-dev/finances
emuniq/filament-browser-notifications
syriable/filament-translator
hungnm28/livewire-form
wenprise/eloquent
crudly/encrypted
fadion/bouncy
cuci/prototurk-sdk
gos/pubsub-router-bundle
cuci/prototurk-sdk-symfony
clementtalleu/easyadmin-markdown-bundle