I'm trying to make connections between objects during object creation. Everything looks fine, but when i try to setAnonParticipant() in private joinTrainingAnon() function i get error
Call to a member function setAnonParticipant() on null
So it looks like repository loading is not working, and $anon is still null.
This is how my code looks like
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;
}