我已经阅读了很多关于此的主题,但我似乎无法找到解决我的问题的方法。
我觉得问题很明显,也许我只是盯着它太久了。
错误是 FatalErrorException: Error: Call to a member function has() on a non-object in /vagrant/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php 第 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 控制器的主控制器
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 控制器。
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;
}
我认为问题在于不存在教义容器,这就是我遇到问题的原因。我不知道如何解决这个问题。
任何帮助表示赞赏。谢谢!
编辑
好的,这就是我改变的。
主控制器保持不变。
DAO 控制器添加了一些东西。
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"]