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

Asked

Viewed 307 times

1

I am studying Doctrine and am adapting a project I had already developed in MVC to work with Doctrine. I am having a certain doubt on how to proceed in relation to the variable $entityManager that I believe in the bootstrap.php to be accessed within a Controller class, i.e. bootstrap.php is instantiated, but inside the Controller I cannot 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 inherit, somehow, my Controllers. But I’m wondering if it’s really necessary.

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

Follows the codes: http://pastebin.com/zUaKnECH


Edit [17/07/2014]

Guys, I couldn’t get it to work by using global. To get it to work I instituted a generic DAO class and inherited it. I got it to work, but I can’t see your methods, like using the method getRepository(), beginning $entityManager-> only that there is no list of inherited and instantiated methods in this variable of type Object.

But if I complete the method call in the same hand, getting like this:

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

He can return the data from the database. That is, I just will not have available the list of methods available by the object, getting kind of lost without knowing what to use in certain situations since I’m still learning to use the FW, forcing me to search the internet all the time in case of these doubts.

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

1 answer

3

In addition to putting your bootstrap class on your controller through the requre/require_once, you also need to put the $entityManager tagged global in your Function, for example:

public function minhafuncao() {
    global $entityManager;
    // resto do método
}
  • I think your answer is enough for that question, but watch out for spelling mistakes...

  • Bacana Eduardo! I needed something very similar (ps: not which mistake?)

  • Leofelipe, nice that helped you. If you solved your problem, please mark the answer as solved.

  • Sorry for the delay in responding... Well, +Eduardofernandes, as I explained, I don’t need to require in my Controller, because I already require in my index.php and control my routes with routing class and .htaccess... in bootstrap.php, which is not a class, I defined $entityManagar as global, but not within a method, as I demonstrate in my presented code, is there a difference? That’s why I didn’t call it off.

  • Dear @Leofelipe, see the following reference that talks about Coo dealing with global variables in functions. Note that there is a need to declare the variable as global. http://php.net/manual/en/language.variables.scope.php

  • People, I managed to put to work in a silly way. I did the update there in the Post.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.