问题
I have below relationship between my entities.
One to many between Institute
-> InstituteCourses
. One to many between Courses
-> InstituteCourses
.
I have avoid making many to many between instiutes and courses because i have alot more fields in instiutecourses entity.
For now it is working well,
class Institutes {
/**
* @ORM\OneToMany(targetEntity="InstitutesCourses", mappedBy="institute", cascade={"all"})
**/
protected $instituteCourses;
}
class Courses {
/**
*
* @ORM\OneToMany(targetEntity="InstitutesCourses", mappedBy="course", cascade={"all"}, orphanRemoval=true)
*/
protected $instituteCourses;
}
class InstitutesCourses {
/**
* @ORM\ManyToOne(targetEntity="Institutes", inversedBy="instituteCourses")
* @ORM\JoinColumn(name="institute_id", referencedColumnName="id")
*/
protected $institute;
/**
* @ORM\ManyToOne(targetEntity="Courses", inversedBy="instituteCourses")
* @ORM\JoinColumn(name="course_id", referencedColumnName="id")
*/
protected $course;
/**
* @var boolean $isPrimary
* @ORM\Column(name="is_primary", type="boolean", nullable=true)
*/
protected $isPrimary;
/**
* @var boolean $verified
* @ORM\Column(name="courses_verified", type="boolean", nullable=true)
*/
protected $verified;
/**
* @var boolean $active
* @ORM\Column(name="courses_active", type="boolean", nullable=true)
*/
protected $active;
/**
*
* @ORM\OneToMany(targetEntity="AcademicSessions", mappedBy="instituteCourse")
*/
protected $academicSession;
}
and the form is
$builder->add('institute','entity', array(
'class'=>'PNC\InstitutesBundle\Entity\Institutes',
'property'=>'name',
'label' => 'Institute'
)
)
->add('course', 'entity', array(
'class' => 'PNC\CoursesBundle\Entity\Courses',
'property' => 'courseTitle',
//'multiple' => true,
'attr' => array(
'class' => 'form-control'
)
))
Now if i want to assign one institute mulitple courses i have to all new Action several time, what i wanted to in single submit multiple courses allocated to one institute. when i set multiple = true in courses field in form. it gives error.
UPDATE -
class Institutes {
/**
* @ORM\OneToMany(targetEntity="NC\InstitutesBundle\Entity\InstitutesCourses", mappedBy="institute", cascade={"all"})
* */
protected $inst;
protected $courses;
public function __construct()
{
$this->inst = new ArrayCollection();
$this->courses = new ArrayCollection();
}
// Important
public function getCourses()
{
$courses = new ArrayCollection();
foreach($this->inst as $ins)
{
$courses[] = $ins->getCourses();
}
return $courses;
}
// Important
public function setCourses($courses)
{
foreach($courses as $c)
{
$po = new InstitutesCourses();
$po->setCourse($c);
$po->setInstitute($this);
$this->addInst($po);
}
}
// Important
public function getCourse()
{
return $this;
}
// Important
public function addInst($InstitutesCourses)
{
$this->inst[] = $InstitutesCourses;
}
// Important
public function removeIns($InstitutesCourses)
{
return $this->inst->removeElement($InstitutesCourses);
}
} And
class Courses {
/**
*
* @ORM\OneToMany(targetEntity="NC\InstitutesBundle\Entity\InstitutesCourses", mappedBy="course", cascade={"all"}, orphanRemoval=true)
*/
protected $instituteCourses;
/**
* @return mixed
*/
public function getInstituteCourses()
{
return $this->instituteCourses;
}
/**
* @param mixed $instituteCourses
*/
public function setInstituteCourses($instituteCourses)
{
$this->instituteCourses = $instituteCourses;
}
}
and the bridge entity.
class InstitutesCourses {
/**
* @ORM\ManyToOne(targetEntity="PNC\InstitutesBundle\Entity\Institutes", inversedBy="instituteCourses")
* @ORM\JoinColumn(name="institute_id", referencedColumnName="id")
*/
protected $institute;
/**
* @ORM\ManyToOne(targetEntity="PNC\CoursesBundle\Entity\Courses", inversedBy="instituteCourses")
* @ORM\JoinColumn(name="course_id", referencedColumnName="id")
*/
protected $course;
/**
* @return mixed
*/
public function getInstitute()
{
return $this->institute;
}
/**
* @param mixed $institute
*/
public function setInstitute($institute)
{
$this->institute = $institute;
}
/**
* @return mixed
*/
public function getCourse()
{
return $this->course;
}
/**
* @param mixed $course
*/
public function setCourse( $course)
{
$this->course = $course;
}
}
I made a form of institute type,
->add('courses' , 'entity' , array(
'class' => 'PNCCoursesBundle:Courses' ,
'property' => 'courseTitle' ,
//'expanded' => true ,
'multiple' => true ,
'attr'=> array(
'class' => 'form-control',
),
'label_attr' => array(
'class' => 'control-label'
),
)
Now When i Go to Create a New It perfectly saves the record, when I update the same record it says.
Attempted to call an undefined method named "getCourses" of class "NC\InstitutesBundle\Entity\InstitutesCourses". Did you mean to call e.g. "getCourse" or "setCourse"?
回答1:
You try mix two different approach. First: to make many to many relation between institutes and courses. Create form for institutes Entity, where courses is collection. In that approach doctrine create records in InstitutesCourses table for each course byself. Second: You create form for InstitutesCourses Entity. One form - one entity. In this case you must use one of the work around solution. One of them: 1. In Form Type make 'courses' as not mapped field ('mapped'=false) 2. After submitting form get array of 'courses' in Controller
$courses=$form->get('courses')->getData();
Make loop for each course in courses with creating InstitutesCourses manually like that:
foreach ($courses as $course) { $institutesCourse=new InstitutesCourses(); $institutesCourse->setCourse($course) ->setInstitutes($institute); $em->$persist($institutesCourse); } $em->flush();
来源:https://stackoverflow.com/questions/33868697/symfony-assign-multiple-values-at-once-in-bridge-entity-form