gpt4 book ai didi

php - fatal error 异常 : Error: Call to a member function has() on a non-object

转载 作者:可可西里 更新时间:2023-11-01 12:42:01 24 4
gpt4 key购买 nike

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

我觉得问题很明显,也许我盯着它看的时间太长了。

错误是 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 Controller 的主 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 Controller 。

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

我认为问题是 Doctrine 容器不存在,这就是我遇到问题的原因。我不确定如何解决这个问题。

感谢任何帮助。谢谢!

编辑


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

主 Controller 保持不变。

添加了一些 DAO Controller 。

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

最佳答案

问题是容器没有被注入(inject)到这里的 Controller 中。

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

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class YourController extends Controller

使用 setter injection 将容器注入(inject) Controller (如果未明确定义为服务)以容器作为参数调用方法 setContainer()

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

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

之后清除缓存。

关于php - fatal error 异常 : Error: Call to a member function has() on a non-object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19281713/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com