Symfony 2 - 无法在私有方法中获取存储库 - 在null上调用成员函数set()

时间:2017-02-02 15:13:27

标签: php symfony

我正在尝试在对象创建期间在对象之间建立连接。 一切看起来都很好,但是当我尝试在私人joinTrainingAnon()函数中setAnonParticipant()时我得到错误

Call to a member function setAnonParticipant() on null

所以看起来存储库加载不起作用,$ anon仍为空。

这就是我的代码的样子

public function newAction(Request $request) {
    $anon = new Anon();

    $em = $this->getDoctrine()->getManager();
    $trainingId = $request->get('template-contactform-id');

    $anon->setFirstName($request->get('template-contactform-name'));
    $anon->setLastName($request->get('template-contactform-surname'));
    $anon->setPhoneNumber($request->get('template-contactform-phone'));
    $anon->setEmail($request->get('template-contactform-email'));                

    $em->persist($anon);        
    $em->flush();

    $anonId = $anon->getId();

    $this->joinTrainingAnon($trainingId, $anonId);        
    $this->sendConfirmationMail($request);

    return $this->redirectToRoute('main_page');
}

private function joinTrainingAnon($trainingId, $anonId) {
    $em = $this->getDoctrine()->getManager();
    //Get the anon
    $anon = $em->getRepository("fitProjectBundle:Anon")->findOneById($anonId);
    //Get training
    $training = $em->getRepository("fitProjectBundle:Training")->findOneById($trainingId);
    //Join training
    $training->setAnonParticipant($anon);
    //Save it to the database
    $em->persist($training);
    $em->flush();        

    return true;
}

0 个答案:

没有答案