Doctrine - Access the $ entityManager variable created in bootstrap.php from within a Class

0

I am studying Doctrine and am adapting a project I had already developed in MVC to work with Doctrine. I'm having some questions on how to proceed with the $ entityManager variable I create in bootstrap.php to be accessed inside a Controller class, bootstrap.php is instantiated, but inside the Controller I can not access it. Looking at the apache log gives the following error:

Fatal error:  Call to a member function getRepository() on a non-object in /var/www/html/projetoTeste/src/Projeto/Core/Controller/GruposController.php on line 12

I'm thinking of creating a bootstrap class just to be able to inherit, in some way, from my Controllers. But I'm in doubt if it's really necessary.

Well, the project structure is like this: I have index.php that requires bootstrap.php , in it I run the Projeto\FrontController::run(); method that controls routing, that is, which classes and views will be called. Making it clear then bootstrap.php will always be run before any class is instantiated.

Follow the codes: link

Edit [17/07/2014]

Personally, I have not been able to get by using global. To put it to work I instantiated a generic DAO class and inherited it. I was able to get it to work, but I can not see its methods, type using the getRepository() method, start $entityManager-> only the list of inherited and instantiated methods does not appear in this Object type variable.

But if I complete the call of the method on the same hand, it looks like this:

$entityManager->getRepository($this->entidade);
return $entityRepository->findAll();

It can return the data from the Database. That is, I will not only have available the list of methods available by the object, getting kind of lost without knowing what to use in certain situations since I am still learning to use FW, forcing me to search the internet in case of these doubts.

Is there any way to force my IDE to display this list? I'm using Netbeans .

    
asked by anonymous 29.06.2014 / 22:57

1 answer

3

In addition to putting your bootstrap class in your controller via requre/require_once , you also need to put $entityManager with the global tag in your function, for example:

public function minhafuncao() {
    global $entityManager;
    // resto do método
}
    
11.07.2014 / 19:58