I have a read a lot of topics on this and I can\'t seem to find a solution to my problem.
I feel like the problem is obvious and maybe I have just been staring at
One more suggestion: when you are moving database connection out of main controller you need to construct new instance of Entity Manager for instance:
class StoreController extends Controller{
public function addstoreAction(Request $request){
$checkStore = new StoreExistsCheck($this ->getDoctrine()->getManager());
//your code here
}
using /myBundle/Model folder
namespace myBundle\Model;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use myBundle\Entity\Store;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Query;
class StoreExistsCheck extends Controller{
protected $em = null;
protected $kernel = null;
public function __construct(EntityManager $em) {
$this->em = $em;
}
public function storeExists($argosStore){
$storeExists = $this->em ->getRepository('myBundle:Store')
->findOneBystoreNumber($argosStore->getStoreNumber());
return $storeExists;
}
Not sure why you would make a controller a service. For what use case? Normally a service is a Plain Old PHP Object.
About your problem .. since you are using the controller as a service it does not get the container automatically. So you have to inject the entire container, which is kind of heavy if you just need doctrine.
So it's better just to inject the things you really need. To inject doctrine, in your yml below class:
arguments: ["@doctrine.orm.entity_manager"]
Then in your controller constructor:
public function __construct($entityManager) {
$this->entityManager = $entityManager;
}
Possible you will need to call the parent constructor (be aware of that).
If you want do inject the complete service container anyway, here is the right section in the manual how you can do that: http://symfony.com/doc/current/cookbook/service_container/scopes.html#passing-the-container-as-a-dependency-of-your-service
The problem is the container not being injected into the controller here.
Normally, Symfony does this automatically if you're extending Symfony\Bundle\FrameworkBundle\Controller\Controller
, which itself extends Symfony\Component\DependencyInjection\ContainerAware
:
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class YourController extends Controller
The container is injected into the controller (if not explicitly defined as a service) using setter injection calling the method setContainer()
with the container as an argument.
Now, as you configured your controller as a service you need to add the setContainer call to your service configuration:
services:
database_controller:
class: Fuel\FormBundle\Controller\DatabaseController
calls:
- [setContainer, ["@service_container"]]
Clear your cache afterwards.