Question

I am trying to set up some embedded forms using this guide. I have two models set up in my application, Lesson and Evaluation.

Each Lesson can have multiple Evaluations.

I have a form set up where the user can create a lesson and also as many evaluations as they want within that lesson. When the form is submitted, it creates the lesson record and all of the evaluation records successfully, however the evaluation records that get created are not linked to the parent lesson (the lesson_id field is just left blank).

Can anyone help?

Any advice appreciated.

Thanks.

My Model classes are set up like this:

Evaluation:

class Evaluation
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\ManyToOne(targetEntity="Lesson", inversedBy="evaluations")
     * @ORM\JoinColumn(name="lesson_id", referencedColumnName="id")
     */
    protected $lesson;

/**
     * Set lesson
     *
     * @param \LessonBundle\Entity\Lesson $lesson
     * @return Evaluation
     */
    public function setLesson(\LessonBundle\Entity\Lesson $lesson = null)
    {
        $this->lesson = $lesson;

        return $this;
    }

    /**
     * Get lesson
     *
     * @return \LessonBundle\Entity\Lesson 
     */
    public function getLesson()
    {
        return $this->lesson;
    }
}

And the Lesson:

class Lesson
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\OneToMany(targetEntity="Evaluation", mappedBy="lesson", cascade={"persist"})
     */
    protected $evaluations;

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


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Add evaluations
     *
     * @param \DS\LessonBundle\Entity\Evaluation $evaluations
     * @return Lesson
     */
    public function addEvaluation(\LessonBundle\Entity\Evaluation $evaluations)
    {
        $this->evaluations[] = $evaluations;

        return $this;
    }

    /**
     * Remove evaluations
     *
     * @param \DS\LessonBundle\Entity\Evaluation $evaluations
     */
    public function removeEvaluation(\LessonBundle\Entity\Evaluation $evaluations)
    {
        $this->evaluations->removeElement($evaluations);
    }

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

    public function setEvaluations(ArrayCollection $evaluations)
    {
        foreach ($evaluations as $evaluation) {
            $evaluation->setLesson($this);
        }

        $this->evaluations = $evaluations;
    }
}

My Controller method:

public function newAction()
{
$lesson = new Lesson;

$evaluation1 = new Evaluation();
$lesson->getEvaluations()->add($evaluation1);
$form    = $this->createForm(new LessonType(), $lesson);
$request = $this->getRequest();

if ($request->getMethod() == 'POST') {
    $form->bindRequest($request);

    if ($form->isValid()) {

        $em = $this->getDoctrine()->getEntityManager();
        $em->persist($lesson);
        $em->flush();

        return $this->redirect($this->generateUrl('lesson_list'));
    }
}

return $this->render('LessonBundle:Lesson:new.html.twig', array('form' => $form->createView()));

}

And my forms:

class LessonType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('evaluations', 'collection', array(
                                            'type' => new EvaluationType(),
                                            'allow_add' => true,
                                            'by_reference' => false,
                                            ));
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => 'LessonBundle\Entity\Lesson',
        );
    }

    public function getName()
    {
        return 'Lesson';
    }
}

And:

class EvaluationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('report');
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => 'LessonBundle\Entity\Evaluation',
        );
    }

    public function getName()
    {
        return 'Evaluation';
    }
}

And finally, my form twig template:

{% extends '::base.html.twig' %}

{% block content %}
<form class="vertical" action="" method="post" {{ form_enctype(form) }}>
    {{ form_errors(form) }}

    <ul class="collectionholder" data-prototype="{{ form_widget(form.evaluations.vars.prototype)|e }}">
    {% for evaluation in form.evaluations %}

        <li>{{ form_row(evaluation) }}</li>

    {% endfor %}
    </ul>
    {{ form_rest(form) }}
    <input type="submit" />
</form>
{% endblock %}
Was it helpful?

Solution

In your class Lesson entity add:

/**
 * Add evaluations
 *
 * @param \DS\LessonBundle\Entity\Evaluation $evaluations
 * @return Lesson
 */
public function addEvaluation(\LessonBundle\Entity\Evaluation $evaluations)
{
    $this->evaluations[] = $evaluations;

    $evaluations->setLesson($this);


    return $this;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top