FatalErrorException: Error: Member function call has () for non-object

I have many questions on this, and I can not find a solution to my problem.

I feel the problem is obvious, and maybe I just looked at it for too long.

Error: FatalErrorException: Error: calling a member function has () for a non-object in the line /vagrant/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php 198

Looking at the error line, he says.

public function getDoctrine() { if (!$this->container->has('doctrine')) { throw new \LogicException('The DoctrineBundle is not registered in your application.'); } return $this->container->get('doctrine'); } 

Here is my code ...

This is the main controller that calls the DAO controller

 public function clickThroughAction(request $request, $hash) { if (isset($hash)) { $dbController = $this->get('database_controller'); print_r($dbController->getReferralIdByHash($hash)); return $this->redirect($this->generateUrl('home')); } else { return 0; } } 

This is the service that is being used.

 services: database_controller: class: Fuel\FormBundle\Controller\DatabaseController 

This is the dao controller that calls the database.

 public function getReferralIdByHash($hash) { $em = $this->getDoctrine()->getManager(); $query = $em->createQuery( 'Select u From FuelFormBundle:UserReferrals u WHERE u.user_hash = :hash' )->setParameter('hash', $hash); $referral = $query->getResult(); if (!$referral) { throw $this->createNotFoundException( 'No product referral found' ); $logger = $this->get('logger'); $logger->info('I just got the logger'); $logger->crit('An error occurred, hash for referrals is not recognized. current hash is: ' . $hash . " ."); return $this->redirect($this->generateUrl('home')); } $clickThroughCount = $referral[0]->getClickThrough(); $referral[0]->setClickThrough($clickThroughCount + 1); $em->flush(); return $referral; } 

I think the problem is that there is no doctrine container, so I am having problems. I am not sure how to solve this.

Any help is appreciated. Thank you

Edit


So here is what I changed.

The main controller remains the same.

DAO Controller added a few things.

 class DatabaseController extends Controller{ protected $entityManager; public function __construct($entityManager) { $this->entityManager = $entityManager; } public function getReferralIdByHash($hash) { $em = $this->entityManager; $query = $em->createQuery( 'Select u From FuelFormBundle:UserReferrals u WHERE u.user_hash = :hash' )->setParameter('hash', $hash); $referral = $query->getResult(); if (!$referral) { throw $this->createNotFoundException( 'No product referral found' ); $logger = $this->get('logger'); $logger->info('I just got the logger'); $logger->crit('An error occurred, hash for referrals is not recognized. current hash is: ' . $hash . " ."); return $this->redirect($this->generateUrl('home')); } $clickThroughCount = $referral[0]->getClickThrough(); $referral[0]->setClickThrough($clickThroughCount + 1); $em->flush(); return $referral; } } 

The service turned out to be similar to this

 services: database_controller: class: Fuel\FormBundle\Controller\DatabaseController arguments: ["@doctrine.orm.entity_manager"] 
+7
php symfony doctrine
source share
3 answers

The problem is that the container is not entered into the controller here.

Typically, Symfony does this automatically if you extend 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 (unless it is explicitly defined as a service) using an installer injection that calls the setContainer() method using the container as an argument.

Now that you have configured your controller as a service, you need to add a setContainer call to your service configuration:

 services: database_controller: class: Fuel\FormBundle\Controller\DatabaseController calls: - [setContainer, ["@service_container"]] 

Remove the cache after that.

+23
source share

Not sure why you should make the controller services. For what occasion? Typically, a service is a regular PHP object.

About your problem .. since you use the controller as a service, it does not automatically receive the container. Thus, you need to introduce the entire container, which is quite heavy if you just need a doctrine.

So it’s better to just enter what you really need. To introduce the doctrine in your yml below the class:

 arguments: ["@doctrine.orm.entity_manager"] 

Then in your controller constructor:

 public function __construct($entityManager) { $this->entityManager = $entityManager; } 

You may need to call the parent constructor (remember this).

If you want to fully add the full service container, here in this section you will find the correct part of the manual: http://symfony.com/doc/current/cookbook/service_container/scopes.html#passing-the-container-as-a -dependency-of-your-service

+1
source share

Another suggestion: when you move the database connection from the main controller, you need, for example, to build a new instance of Entity Manager:

 class StoreController extends Controller{ public function addstoreAction(Request $request){ $checkStore = new StoreExistsCheck($this ->getDoctrine()->getManager()); //your code here } 

using the folder / myBundle / Model

 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; } 
+1
source share

All Articles