PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, February 15, 2022

[FIXED] foreign key null in collection form symfony 3

 February 15, 2022     doctrine-orm, php, symfony, symfony-forms     No comments   

Issue

i have two entities Survey.php and Choice.php I create a form to add new survey and multi choices, I used a many-to-one relation between the two entities, the problem is when I submit for a new survey, the foreign key of choice entity return null

here's my code

Survey.PHP

/**
 * Survey
 *
 * @ORM\Table(name="survey")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\SurveyRepository")
 */
class Survey
{
 /....  

    /**
     * @var ArrayCollection
     * @ORM\OneToMany(targetEntity="AppBundle\Entity\Choice", mappedBy="survey_id",cascade="persist")
     * @ORM\JoinColumn(nullable=false, referencedColumnName="id")
     */
    private $choice;

    public function __construct()
    {
        $this->choice = new ArrayCollection();
    }



    /**
     * Add choice
     *
     * @param \AppBundle\Entity\Choice $choice
     *
     * @return Survey
     */
    public function addChoice(\AppBundle\Entity\Choice $choice)
    {
        $this->choice[] = $choice;
        $choice->setSurvey($this);
        return $this;
    }

    /**
     * Remove choice
     *
     * @param \AppBundle\Entity\Choice $choice
     */
    public function removeChoice(\AppBundle\Entity\Choice $choice)
    {
        $this->choice->removeElement($choice);
    }

    /**
     * Get choice
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getChoice()
    {
        return $this->choice;
    }
}

Choice.php

/**
 * Choice
 * @ORM\Table(name="choice")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\ChoiceRepository")
 */
class Choice
{

    /**
     * @var int
     * @ORM\ManyToOne(targetEntity="Survey",inversedBy="choice")
     */
    private $survey;



    /**
     * Set survey
     *
     * @param \AppBundle\Entity\Survey $survey
     *
     * @return Choice
     */
    public function setSurveyId(\AppBundle\Entity\Survey $survey)
    {
        $this->survey = $survey;

        return $this;
    }

    /**
     * Get surveyId
     *
     * @return \AppBundle\Entity\Survey
     */
    public function getSurveyId()
    {
        return $this->survey_id;
    }
}

SurveyController.php

<?php

namespace AppBundle\Controller;

use AppBundle\Entity\Survey;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

/**
 * Survey controller.
 *
 */
class SurveyController extends Controller
{

    /**
     * Creates a new survey entity.
     * @param Request $request
     * @return \Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
     */
    public function newAction(Request $request)
    {
        $survey = new Survey();
        //
        $form = $this->createForm('AppBundle\Form\SurveyType', $survey);
        $form->handleRequest($request);
        $survey->setCreateDate(new \DateTime('NOW'));

        if ($form->isSubmitted() && $form->isValid()) {
                $em = $this->getDoctrine()->getManager();
                $em->persist($survey);
                $em->flush();


                return $this->redirectToRoute('survey_show', ['id' => $survey->getId()]);
            }


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

any suggestion, btw I think the problem is in the getters and setters )


Solution

I fixed the problem by adding a for each loop inside the SurveyController.php and it works just fine

SurveyController.php

if ($form->isSubmitted() && $form->isValid())
        {
            foreach ($survey->getChoices() as $choice){
                $choice->setSurvey($survey);
            }

            $em = $this->getDoctrine()->getManager();
            $em->persist($survey);
            $em->flush();

not "THE best solution" but it gets the job done



Answered By - Belkacem Yahiaoui
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing