FatalErrorException:错误:调用成员函数has()在非对象上 [英] FatalErrorException: Error: Call to a member function has() on a non-object

查看:242
本文介绍了FatalErrorException:错误:调用成员函数has()在非对象上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了很多关于这个问题的主题,我似乎找不到解决我的问题的办法。

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:错误:调用成员函数has()在/ vagrant / vendor / symfony / symfony / src / Symfony / Bundle中的非对象/FrameworkBundle/Controller/Controller.php行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:  Fuel\FormBundle\Controller\DatabaseController

这是调用数据库的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.

任何帮助都不胜感激。谢谢!

Any help is appreciated. Thanks!

编辑

好的,所以这里是我改变的。主控台保持不变,

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:  Fuel\FormBundle\Controller\DatabaseController
          arguments: ["@doctrine.orm.entity_manager"]


推荐答案

是容器没有被注入到控制器中。

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

通常,Symfony会自动执行此操作,如果您正在扩展 Symfony\Bundle\FrameworkBundle \Controller\Controller ,其本身扩展 Symfony\Component\DependencyInjection\ContainerAware

Normally, Symfony does this automatically if you're extending Symfony\Bundle\FrameworkBundle\Controller\Controller, which itself extends Symfony\Component\DependencyInjection\ContainerAware:

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class YourController extends Controller

将容器注入控制器(如果未明确定义为服务)使用设置注入调用方法 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:  Fuel\FormBundle\Controller\DatabaseController
        calls:
            - [setContainer, ["@service_container"]]

稍后清除缓存。

这篇关于FatalErrorException:错误:调用成员函数has()在非对象上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆