Symfony2/JmsDIExtraBundle Injecting repository into service using annotations


In my project I’m using annotations with JMSDIExtraBundle. My question is: how I can tell my app that repository should be service so, I can inject it in another service using annotations. <strong>Only way I know is using XML file to define repository as service.</strong> But it’s a very slow process(comparing it to simple @DI\Service which I prefer over defining it in yml or xml files).

I found an alternative solution to XML but I think it's bad idea:

####CustomService.php#### /** * @param EntityManager $em * * @DI\InjectParams({ * "em" = @DI\Inject("doctrine.orm.entity_manager") * }) */ public function __construct(EntityManager $em) { $this->em = $em; }

And somewhere in service:

$entityRepository = $this->em->getRepository(AcmeBundle:Entity);

Any ideas how I can speed up coding process when I need to use repository?


You can define your entity repository as a service and inject it into the service.

As Example, you can configure the repo as a service like:

In a standard Symfony Service definition Way:

####service.xml#### <service id="acme.user.repository" class="Doctrine\ORM\EntityRepository" factory-service="doctrine.orm.entity_manager" factory-method="getRepository"> <argument>AcmeDemoBundle:User</argument> </service>

Or using the factory to Service annotation defined in the JMSDiExtraBundle (see <a href="https://github.com/schmittjoh/JMSDiExtraBundle/pull/115" rel="nofollow">this</a>):

/** * @Service("acme.user.repository", factoryService = "doctrine", factoryMethod="getRepository", factoryMethodArguments={ * "persistentObjectName" = "Acme\DemoBundle\Entity\User" * } ) */

And Inject and use it as:

####CustomService.php#### /** * @param Doctrine\ORM\EntityRepository $repo * * @DI\InjectParams({ * "repo" = @DI\Inject("acme.user.repository") * }) */ public function __construct(EntityRepository $repo) { $this->repo = $repo; }

Is not a great speedup, but permit to only inject what you need

Hope this help


  • How to add custom system service name to @ServiceName StringDef?
  • android displaying multiple activity in saved state when notification click
  • JPA Mapping Multi-Rows with ElementCollection
  • Exception while creating EntityManagerFactory - Lookup failed for 'persistence/myPU' in Se
  • JAVA EE Stateless EJB IllegalArgumentException: Can not set field
  • How to take advantage of kernel.terminate inside an event listener
  • startup class in Weld
  • Applying custom annotation advice to spring data jpa repository
  • How to get the index of element in the List in c#
  • command line of process by name
  • Primefaces lazy datascroller calling load twice
  • Doctrine2 bulk import try to work with another entity
  • Passing “get” parameters doesn't work, parameter not visible in the link
  • Assign variable to the value in HTML
  • Declaring variable dynamically in VB.net
  • Xcode 4 NSLog Macro link in Xcode 3
  • Play WS (2.2.1): post/put large request
  • Bad request using file_get_contents for PUT request in PHP
  • Read text file and split every line in MSBuild
  • C# - Serializing and deserializing static member
  • Excel - Autoshape get it's name from cell (value)
  • Java applet as stand-alone Windows application?
  • Submit form in a displaytag pagination
  • Check if a string to interpolate provides expected placeholders
  • Deserializing XML into class C#
  • Upload files with Ajax and Jquery
  • Do I've to free mysql result after storing it?
  • RestKit - RKRequestDelegate does not exist
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • How do I rollback to a specific git commit
  • Is there a mandatory requirement to switch app.yaml?
  • Traverse Array and Display in markup
  • A cron job substitute?
  • Android Studio and gradle
  • json Serialization in asp
  • SQL merge duplicate rows and join values that are different
  • Error creating VM instance in Google Compute Engine
  • Linking SubReports Without LinkChild/LinkMaster
  • Busy indicator not showing up in wpf window [duplicate]
  • How to Embed XSL into XML