我正在尝试为动态表单执行以下过程:
- 用户选择学校
- 然后,我需要获取与该学校相关的所有“成绩”。
我试图用这种形式实现的是创建一个新的“班级”(如学校班级),它与成绩相关联。问题(我认为,正如您将在下面看到的那样)是 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,
));
}
);
任何帮助将不胜感激!