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.
Installation
Add the bundle to composer.json:
"require": {
"activpik/entity-manager-generator-bundle": "dev-master"
}
Run:
composer update
Register the Bundle
Add to app/AppKernel.php:
new Activpik\EntityManagerGeneratorBundle\ActivpikEntityManagerGeneratorBundle(),
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/.
Use Case: CRUD Operations with a Dedicated Manager
Generate a Manager
php app/console doctrine:generate:entitymanager AcmeDemoBundle:Product
This creates ProductManager.php in src/Acme/DemoBundle/Entity/.
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
// ...
}
}
Manager Generation Workflow
src/BundleName/Entity/ for consistency.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]);
}
}
Service Integration
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>
$manager = $this->get('acme_demo.product_manager');
Repository Access
getRepository() method to interact with Doctrine repositories:
$repository = $manager->getRepository();
$product = $repository->find(1);
Symfony Forms Integrate managers with Symfony forms for validation and data handling:
$form = $this->createFormBuilder($manager->createNew())
->add('name', TextType::class)
->getForm();
Event Subscribers Attach event subscribers to managers for pre/post operations:
$manager->getEntityManager()->getEventManager()->addEventSubscriber(new ProductSubscriber());
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);
Services.xml Requirement
services.xml. If your bundle uses autowiring or YAML, this bundle will fail silently or throw errors.services.xml or avoid using this bundle.Generated Manager Overwriting
Namespace Conflicts
src/Bundle/Entity/Model/Product), the manager will be generated in src/Bundle/Entity/Model/, which may not align with your project structure.Manager directory if needed.Doctrine Lifecycle Events
prePersist). Add these manually:
$manager->getEntityManager()->getEventManager()->addEventListener(
[Product::class, 'ProductManager'],
new ProductLifecycleListener()
);
Command Not Found
AppKernel.php and composer.json is updated.php app/console list to verify the command exists.Manager Not Autowired
services.xml as shown above.php app/console debug:container for the service ID.Repository Not Found
// Correct:
$repository = $manager->getRepository('AcmeDemoBundle:Product');
// Incorrect (if entity is in a sub-namespace):
$repository = $manager->getRepository('Acme\DemoBundle\Entity\Model\Product');
Custom Manager Templates
vendor/activpik/entity-manager-generator-bundle/Resources/views/.ProductManager.php.twig in your bundle’s Resources/views/ directory.Adding Common 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);
}
}
BaseManager.Dynamic Manager Generation
ManagerGenerator service programmatically to generate managers dynamically:
$generator = $this->get('activpik_entity_manager_generator.manager_generator');
$generator->generate('AcmeDemoBundle:Product');
Bundle Prefix in Service IDs
bundle_prefix.manager.entity_name. Example:
AcmeDemoBundle:Product, the service ID is acme_demo.product_manager.php app/console debug:container to confirm the correct service ID.Entity Manager Injection
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>
How can I help you explore Laravel packages today?