对null Symfony调用成员函数persist()



我开始使用Symfony进行开发,在代码中出现以下错误:错误

这是我的控制器:

class MatierePremiereController extends AbstractController
{
/**
* @var EntityManagerInterface
*/
private $em;
public function __construc(EntityManagerInterface $em){
$this->em = $em;
}

/**
* @Route("/matiere_premiere", name="mp.index")
* @param MatierePremiereRepository $repository
* @return Response
*/
public function index(MatierePremiereRepository $repository): Response
{
$mp= $repository->findAll();
return $this->render('matiere_premiere/index.html.twig', [
'matierepremieres' => $mp
]);
}

/**
* @Route("/matiere_premiere/new", name="matierepremiere.new")
* @param Request $request
* @return Response
*/

public function new(Request $request): Response
{
$ez = new MatierePremiere();
$ez = ['$ref'=>31, '$unite_mesure'=>'cm', '$stock'=>30, '$seuil_alert'=>4, '$seuil_critique'=>2, '$prix_ht'=>30];
$this->em->persist($ez);
$mp = new MatierePremiere();
$form = $this->createForm(MatierePremiereFormType::class, $mp);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
//:dd($mp);
$this->em->persist($mp);
$this->em->flush();
return $this->redirectToRoute('matierepremiere.list');

}
return $this->render('matiere_premiere/new.html.twig', [
'matierepremiere' => $mp,
'form' => $form->createView()
]);
}

(使用"$ez"是为了测试,persist((错误仍然存在(

我想要一些帮助,我已经尝试了在论坛上找到的几个解决方案,但没有任何帮助!

错误是__construct((方法的名称,看看这个:

public function __construc(EntityManagerInterface $em){
$this->em = $em;
}

请修复方法名称

最新更新