Skip to content

Instantly share code, notes, and snippets.

@bigcalm
Created February 4, 2013 08:41
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save bigcalm/4705662 to your computer and use it in GitHub Desktop.
Save bigcalm/4705662 to your computer and use it in GitHub Desktop.
Making an entity repository container aware in Symfony2.1
<?php
namespace Acme\DemoBundle\Repository;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Doctrine\ORM\EntityRepository;
/**
* FooRepository
*
* @author Iain Cuthbertson <iain.cuthbertson@siftware.com>
*/
class FooRepository extends EntityRepository implements ContainerAwareInterface
{
/**
* @var ContainerInterface
*/
private $container;
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
/**
* Example method that uses a container service
*
* @param integer $parentId
* @return \Acme\DemoBundle\Entity\Foo
*/
public function getOrCreate($controllerId)
{
$foo = new Foo();
$exampleTools = $this->container->get('exampleTools');
$exampleTools->someMethod($foo);
return $foo;
}
}
@mysiar
Copy link

mysiar commented Mar 20, 2017

Does this require service defintion and injection of ["@service_container"] ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment