5

我正在尝试使用指南设置一些嵌入式表单。我在我的应用程序中设置了两个模型,课程和评估。

每个课程可以有多个评估。

我设置了一个表单,用户可以在其中创建课程以及该课程中所需的任意数量的评估。提交表单后,它会成功创建课程记录和所有评估记录,但是创建的评估记录未链接到父课程(课程 ID 字段留空)。

任何人都可以帮忙吗?

任何建议表示赞赏。

谢谢。

我的模型类是这样设置的:

评估:

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;
    }
}

和教训:

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;
    }
}

我的控制器方法:

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()));

}

我的表格:

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';
    }
}

和:

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';
    }
}

最后,我的表单树枝模板:

{% 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 %}
4

1 回答 1

10

在您的课程实体中添加:

/**
 * 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;
}
于 2013-01-26T16:07:02.853 回答