I am trying to customize some inline forms using this guide. I have two models created in my application, "Lesson and Assessment".
Each lesson can have several grades.
I have a form in which the user can create a lesson, as well as as many grades as they want in this lesson. When the form is submitted, it successfully creates the lesson record and all the grade records, however the created grade records are not associated with the parent lesson (the lesson_id field simply remained empty).
Can anyone help?
Any advice is appreciated.
Thank.
My model classes are configured as follows:
Rating:
class Evaluation
{
private $id;
protected $lesson;
public function setLesson(\LessonBundle\Entity\Lesson $lesson = null)
{
$this->lesson = $lesson;
return $this;
}
public function getLesson()
{
return $this->lesson;
}
}
And Lesson:
class Lesson
{
private $id;
protected $evaluations;
public function __construct()
{
$this->evaluations = new ArrayCollection();
}
public function getId()
{
return $this->id;
}
public function addEvaluation(\LessonBundle\Entity\Evaluation $evaluations)
{
$this->evaluations[] = $evaluations;
return $this;
}
public function removeEvaluation(\LessonBundle\Entity\Evaluation $evaluations)
{
$this->evaluations->removeElement($evaluations);
}
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 are:
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 branch shape 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 %}