我正在尝试在对象创建期间在对象之间建立连接。 一切看起来都很好,但是当我尝试在私人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;
}