Symfony表单事件,但“数据”不是表单实体的一部分

时间:2013-11-09 20:09:40

标签: php symfony doctrine-orm symfony-forms

我正在尝试为动态表单执行以下过程:

  1. 用户选择学校
  2. 然后我需要获得与该学校相关的所有“成绩”。
  3. 我试图通过这种形式实现的是创建一个新的“班级”(如学校班级),它与一个年级相关联。问题(我认为,正如您将在下面看到的)是SchoolClass对象不知道School对象,只知道Grade对象。我只发布相关代码......

    学校班级

    /**
     * @ORM\OneToMany(targetEntity="Grade", mappedBy="school")
     */
    private $grades;
    public function __construct()
    {
        $this->grades = new \Doctrine\Common\Collections\ArrayCollection();
    }
    
    /**
     * Add grades
     *
     * @param \MyBundle\MainBundle\Entity\Grade $grades
     * @return School
     */
    public function addGrade(\MyBundle\MainBundle\Entity\Grade $grades)
    {
        $this->grades[] = $grades;
    
        return $this;
    }
    
    /**
     * Remove grades
     *
     * @param \MyBundle\MainBundle\Entity\Grade $grades
     */
    public function removeGrade(\MyBundle\MainBundle\Entity\Grade $grades)
    {
        $this->grades->removeElement($grades);
    }
    
    /**
     * Get grades
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getGrades()
    {
        return $this->grades;
    }
    

    年级

    /**
     * @var School
     *
     * @ORM\ManyToOne(targetEntity="School", inversedBy="grades")
     * @ORM\JoinColumn(name="school_id", referencedColumnName="id")
     */
    private $school;
    
    /**
     * @ORM\OneToMany(targetEntity="SchoolClass", mappedBy="grade")
     */
    private $schoolClasses;
    
    //Getters and setters for school come here, leaving them out as they are the usual...
    
    
    /**
     * Constructor
     */
    public function __construct()
    {
        $this->schoolClasses = new \Doctrine\Common\Collections\ArrayCollection();
    }
    
    /**
     * Get schoolClasses
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getSchoolClasses()
    {
        return $this->schoolClasses;
    }
    
    /**
     * Add schoolClasses
     *
     * @param \MyBundle\MainBundle\Entity\SchoolClass $schoolClasses
     * @return Grade
     */
    public function addSchoolClasse(\MyBundle\MainBundle\Entity\SchoolClass $schoolClasses)
    {
        $this->schoolClasses[] = $schoolClasses;
    
        return $this;
    }
    /**
     * Remove schoolClasses
     *
     * @param \MyBundle\MainBundle\Entity\SchoolClass $schoolClasses
     */
    public function removeSchoolClasse(\MyBundle\MainBundle\Entity\SchoolClass $schoolClasses)
    {
        $this->schoolClasses->removeElement($schoolClasses);
    }
    

    顺便说一句,removeSchoolClasse()addSchoolClasse()函数就像Doctrine一样生成,这是一个错误吗?

    编辑 - 忘了添加SchoolClass类......

    /**
     * @var Grade
     *
     * @ORM\ManyToOne(targetEntity="Grade", inversedBy="schoolClasses")
     * @ORM\JoinColumn(name="grade_id", referencedColumnName="id")
     */
    private $grade;
    /**
     * Set grade
     *
     * @param Grade $grade
     * @return SchoolClass
     */
    public function setGrade($grade)
    {
        $this->grade = $grade;
    
        return $this;
    }
    
    /**
     * Get gradeId
     *
     * @return integer
     */
    public function getGrade()
    {
        return $this->grade;
    }
    

    无论如何,我尝试过关于Symfony文档的教程,但这没有用,所以我尝试了自己的表单事件,其重要部分在这里:

        $builder
                ->add('classDescription')
                ->add('school', 'entity', array(
                    'class' => 'MyBundleMainBundle:School',
                    'multiple' => false,
                    'mapped' => false,
                ));
    
            //Add event listener for when user changes the school...
            $builder->addEventListener(
                FormEvents::PRE_SET_DATA,
                function(FormEvent $event) {
                    $form = $event->getForm();
                    //Here I tried my own implementation but obviously doesn't work as $school will be of type Form, am I correct?
                    $school = $form->get('school');
                    $grades = $school->getGrades();
    
                    $form->add('grade', 'entity', array(
                        'choices' => $grades,
                    ));
                }
            );
    

    任何帮助都将受到高度赞赏!

1 个答案:

答案 0 :(得分:0)

表单事件不会在另一个字段的函数中填充字段,您必须使用javascript来执行此操作。

但是如果你想通过验证过程,你应该使用像documentation中的describe这样的表单事件。