Created
February 4, 2013 08:41
-
-
Save bigcalm/4705662 to your computer and use it in GitHub Desktop.
Making an entity repository container aware in Symfony2.1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does this require service defintion and injection of ["@service_container"] ?