FatalErrorException:错误:在非对象上调用成员函数 has()

2022-01-16 00:00:00 php symfony doctrine

我已经阅读了很多关于此的主题,但我似乎无法找到解决问题的方法.

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.

错误是 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

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

查看错误行,它说.

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

        return $this->container->get('doctrine');
    }

这是我的代码...

这是调用 DAO 控制器的主控制器

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;

        }
    }

这是正在使用的服务.

services:
    database_controller:
        class:  FuelFormBundleControllerDatabaseController

这是调用数据库的 dao 控制器.

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.

感谢任何帮助.谢谢!

编辑

好的,这就是我所做的更改.

Ok so here is what I changed.

主控制器保持不变.

DAO 控制器添加了一些东西.

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

服务最终看起来像这样

services:
     database_controller:
          class:  FuelFormBundleControllerDatabaseController
          arguments: ["@doctrine.orm.entity_manager"]

推荐答案

问题是这里没有将容器注入到控制器中.

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

通常,如果您扩展 SymfonyBundleFrameworkBundleControllerController,Symfony 会自动执行此操作,它本身扩展 SymfonyComponentDependencyInjectionContainerAware:

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

use SymfonyBundleFrameworkBundleControllerController;

class YourController extends Controller

使用 setter injection 调用方法 setContainer() 以容器为参数.

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.

现在,当您将控制器配置为服务时,您需要将 setContainer 调用添加到服务配置中:

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"]]

之后清除缓存.

相关文章