A little background information. I have two entities that have a Bidirectional relationship: essentially, an application form can have exactly one recommendation form. When a recommender fills out a form, I would like to have it automatically, and only select the related application form (protect other user's data), based on a specific code, which they enter on a custom formpage I created. I'm not sure how it happened, but I got it to work once, but for some unknown reason, it stopped and is adding "NULL" for the applicant form id in the recommendation form in my database. I'll include all info that I can:
RecommendationForm Entity:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* RecommendationForm
*
* @ORM\Table(name="recommendation_form")
* @ORM\Entity(repositoryClass="AppBundle\Repository\RecommendationFormRepository")
*/
class RecommendationForm
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* One RecommendationForm has One ApplicantForm.
* @ORM\OneToOne(targetEntity="ApplicantForm", inversedBy="recommendation_form")
* @ORM\JoinColumn(name="applicant_form_id", referencedColumnName="id")
*/
private $applicant_form;
//....
public function __construct($applicant_form) {
date_default_timezone_set('America/New_York');
$this->applicant_form = $applicant_form;
$this->submitDate = new \DateTime("now");
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
// /**
// * Set applicantForm
// *
// * @param integer $applicant_form
// *
// * @return RecommendationForm
// */
// public function setApplicantForm($applicant_form)
// {
// $this->applicant_form = $applicant_form;
// return $this;
// }
/**
* Get applicantForm
*
* @return integer
*/
public function getApplicantForm()
{
return $this->applicant_form;
}
//....
}
ApplicantForm Entity:
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use chriskacerguis\Randomstring\Randomstring;
/**
* ApplicantForm
*
* @ORM\Table(name="applicant_form")
* @ORM\Entity(repositoryClass="AppBundle\Repository\ApplicantFormRepository")
*/
class ApplicantForm
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* One ApplicantForm has One RecommendationForm.
* @ORM\OneToOne(targetEntity="RecommendationForm", mappedBy="applicant_form")
*/
private $recommendation_form;
/**
* @var string
*
* @ORM\Column(name="accessCode", type="string", length=75, unique=true)
*/
private $accessCode;
//....
public function __construct() {
$random = new \chriskacerguis\Randomstring\Randomstring();
$this->accessCode = $random->generate(15, true);
}
//...
}
RecommendationController:
namespace AppBundle\Controller;
use AppBundle\Entity\RecommendationForm;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
/**
* Recommendationform controller.
*
* @Route("recommendationform")
*/
class RecommendationFormController extends Controller
{
private $accessCode;
private $applicantForm;
private $applicantFormID;
/**
* Lists all RecommendationForm entities.
*
* @Route("/", name="recommendationform_index")
* @Method({"GET", "POST"})
*/
public function indexAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$data = array();
$form = $this->createFormBuilder($data)
->add('email', EmailType::class)
->add('accessCodeSS', PasswordType::class)
->add('submit', SubmitType::class)
->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$accessCode = $form["accessCodeSS"]->getData();
return $this->redirectToRoute('recommendationform_new');
}
return $this->render('recommendationform/index.html.twig', array(
'form' => $form->createView(),
));
}
/**
* Creates a new RecommendationForm entity.
*
* @Route("/new", name="recommendationform_new")
* @Method({"GET", "POST"})
*/
public function newAction(Request $request)
{
if(isset($_POST["accessCodeSS"])){
$this->accessCode = $_POST["accessCodeSS"];
$accessCode = $this->accessCode;
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery('SELECT u FROM AppBundle:ApplicantForm u WHERE u.accessCode = :accessCode')
->setParameter('accessCode', $accessCode);
$applicantFormID = $query->getResult();
$this->applicantForm = $applicantFormID[0];
}
var_dump($this->applicantForm);
$RecommendationForm = new Recommendationform($this->applicantForm);
$form = $this->createForm('AppBundle\Form\RecommendationFormType', $RecommendationForm);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($RecommendationForm);
$em->flush();
return $this->redirectToRoute('recommendationform_show', array('id' => $RecommendationForm->getId()));
}
return $this->render('recommendationform/new.html.twig', array(
'RecommendationForm' => $RecommendationForm,
'form' => $form->createView(),
));
}
/**
* Finds and displays a RecommendationForm entity.
*
* @Route("/{id}", name="recommendationform_show")
* @Method("GET")
*/
public function showAction(RecommendationForm $RecommendationForm)
{
$deleteForm = $this->createDeleteForm($RecommendationForm);
return $this->render('recommendationform/show.html.twig', array(
'RecommendationForm' => $RecommendationForm,
'delete_form' => $deleteForm->createView(),
));
}
/**
* Displays a form to edit an existing RecommendationForm entity.
*
* @Route("/{id}/edit", name="recommendationform_edit")
* @Method({"GET", "POST"})
*/
public function editAction(Request $request, RecommendationForm $RecommendationForm)
{
$deleteForm = $this->createDeleteForm($RecommendationForm);
$editForm = $this->createForm('AppBundle\Form\RecommendationFormType', $RecommendationForm);
$editForm->handleRequest($request);
if ($editForm->isSubmitted() && $editForm->isValid()) {
$this->getDoctrine()->getManager()->flush();
return $this->redirectToRoute('recommendationform_edit', array('id' => $RecommendationForm->getId()));
}
return $this->render('recommendationform/edit.html.twig', array(
'RecommendationForm' => $RecommendationForm,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
));
}
/**
* Deletes a RecommendationForm entity.
*
* @Route("/{id}", name="recommendationform_delete")
* @Method("DELETE")
*/
public function deleteAction(Request $request, RecommendationForm $RecommendationForm)
{
$form = $this->createDeleteForm($RecommendationForm);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->remove($RecommendationForm);
$em->flush();
}
return $this->redirectToRoute('recommendationform_index');
}
/**
* Creates a form to delete a RecommendationForm entity.
*
* @param RecommendationForm $RecommendationForm The RecommendationForm entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createDeleteForm(RecommendationForm $RecommendationForm)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('recommendationform_delete', array('id' => $RecommendationForm->getId())))
->setMethod('DELETE')
->getForm()
;
}
}
I have scoured all over the internet and just can't find an answer or figure out how it stopped working.