Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Symfony Container in an EntityRepository

Tags:

php

symfony

I've set a variable in parameters.ini, but now I want to retrieve that variable from an EntityRepository and $this->container is unset so I can't do it

How should I get to the container?

Thanks :)

like image 979
Marcos García Avatar asked Apr 01 '12 01:04

Marcos García


2 Answers

You should not use $container in the EntityRepository. Instead, create a Model Manager service and inject the container through DI.

like image 187
Tuong Le Avatar answered Oct 11 '22 23:10

Tuong Le


Bro, Symfony sometimes or lot of times is a headache, here is a hacky way, is not the correct like the @Tuong Le answer but is a horror do a lot for just a variable like was says @keyboardSmasher.

At the start of the function/method:

global $kernel;
if($kernel instanceOf \AppCache) $kernel = $kernel->getKernel();

So you can acces a container with

$kernel->getContainer();

hope this gives you time to go to walk in the park =),

like image 24
Felipe Buccioni Avatar answered Oct 11 '22 22:10

Felipe Buccioni