Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
356 views
in Technique[技术] by (71.8m points)

php - FatalErrorException: Error: Call to a member function has() on a non-object

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 it too long.

The error is FatalErrorException: Error: Call to a member function has() on a non-object in /vagrant/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php line 198

Looking at the error line, it 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 is calling 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:  FuelFormBundleControllerDatabaseController

This is the dao controller that is calling 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 the doctrine container is not present which is why I am having issues. I am not sure how to solve this.

Any help is appreciated. Thanks!

Edit


Ok so here is what I changed.

Main Controller stayed the same.

DAO Controller a couple of things were added.

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;
    }
}

Service ended up looking like this

services:
     database_controller:
          class:  FuelFormBundleControllerDatabaseController
          arguments: ["@doctrine.orm.entity_manager"]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem is the container not being injected into the controller here.

Normally, Symfony does this automatically if you're extending SymfonyBundleFrameworkBundleControllerController, which itself extends SymfonyComponentDependencyInjectionContainerAware:

use SymfonyBundleFrameworkBundleControllerController;

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:  FuelFormBundleControllerDatabaseController
        calls:
            - [setContainer, ["@service_container"]]

Clear your cache afterwards.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...