本文介绍了坚持使用嵌入式表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



我有一个表单设置,用户可以在该课程中创建课程,以及他们想要的评估。当表单提交时,它会成功创建课程记录和所有评估记录,但是创建的评估记录不链接到父课程(lesson_id字段只剩下空白)。



任何人都可以帮忙?



任何建议赞赏。



谢谢。 p>

我的模型类设置如下:



评估:

  class评估
{
/ **
* @var整数
*
* @ ORM\Column (name =id,type =integer)
* @ ORM\Id
* @ ORM\GeneratedValue(strategy =AUTO)
* /
私人$ id;

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

/ **
*设置课程
*
* @param \LessonBundle\Entity\Lesson $ lesson
* @return评估
* /
public function setLesson(\LessonBundle\Entity\Lesson $ lesson = null)
{
$ this-> lesson = $ lesson;

return $ this;
}

/ **
*获取课程
*
* @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 =评估,mappedBy =课程,cascade = {坚持})
* /
保护$评估;

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


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

/ **
*添加评估
*
* @param \DS\LessonBundle\Entity\Evaluation $ evaluationations
* @return课程
* /
public function addEvaluation(\LessonBundle\Entity\Evaluation $ evaluationations)
{
$ this-> evaluateations [] = $评估;

return $ this;
}

/ **
*删除评估
*
* @param \DS\LessonBundle\Entity\Evaluation $ evaluationations
* /
public function removeEvaluation(\LessonBundle\Entity\Evaluation $ evaluationations)
{
$ this-> evaluateations-> removeElement($ evaluationations);
}

/ **
*获取评估
*
* @return \Doctrine\Common\Collections\Collection
* /
public function getEvaluations()
{
return $ this-> evaluationations;
}

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

$ this-> evaluateations = $ evaluationations;
}
}

我的控制器方法:

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

}



我的表单: / p>

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

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

public function getName()
{
returnLesson';
}
}

And:

  class EvaluationType extends AbstractType 
{
public function buildForm(FormBuilderInterface $ builder,array $ options)
{
$ builder->添加( '报告');
}

public function getDefaultOptions(array $ options)
{
返回数组(
'data_class'=>'LessonBundle\Entity\评估',
);
}

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

最后,我的表格twig模板:

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

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

< ul class =collectionholderdata-prototype ={{form_widget(form.evaluations.vars.prototype)| e}} >
{%for form.evaluations%}

< li> {{form_row(评估)}}< / li>

{%endfor%}
< / ul>
{{form_rest(form)}}
< input type =submit/>
< / form>
{%endblock%}


解决方案

课程实体添加:

  / ** 
*添加评估
*
* @ param \DS\LessonBundle\Entity\Evaluation $ evaluationations
* @return Lesson
* /
public function addEvaluation(\LessonBundle\Entity\Evaluation $ evaluationations)
{
$ this-> evaluateations [] = $ evaluationations;

$ evaluateations-> setLesson($ this);


return $ this;
}


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 %}
解决方案

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

这篇关于坚持使用嵌入式表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 00:38