【发布时间】:2021-12-10 18:41:18
【问题描述】:
我在 symfony 3.4 项目中遇到错误。 我正在尝试在我的应用程序菜单中管理通知的显示。 所以我创建了一个扩展 Controller 的 CustomController。
然后我让所有其他控制器都继承自 CustomController。
但是当我调用 getDoctrine() 以访问存储库时,我收到以下错误:
"调用一个成员函数 has() on null"
这是我的自定义控制器:
<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class CustomController extends Controller
{
public $data = [];
protected $em;
public function __construct()
{
$this->em = $this->getDoctrine()->getManager();
$countAttente = $this->em->getRepository('AppBundle:Commandes')->tailleEnAttente("En attente");
$this->data['countAttente'] = $countAttente;
}
}
我尝试在 service.yml 中将控制器作为服务传递,但它没有改变任何东西
AppBundle\Controller\CustomController:
class: AppBundle\Controller\CustomController
arguments: ["@doctrine.orm.entity_manager"]
calls:
- [setContainer, ["@service_container"]]
我发现了许多关于此类错误的类似主题,但没有一个允许我跳过此错误
欢迎任何帮助
【问题讨论】:
标签: php symfony doctrine-orm