I did some research, and after reading this and this (and all the related questions) I still can\'t figure which is the proper way to update a many to many relationship in S
ManyToMany Bidirectional with indexBy attribute on the annotation fixed this for me
Student class annotation should be
class student_main
{
/**
* @ORM\ManyToMany(targetEntity="support_log", mappedBy="student_main")
**/
private $support_log;
Support class annotation should be
class support_log
{
/**
* @ORM\ManyToMany(targetEntity="student_main", inversedBy="support_log", indexBy="id")
* @ORM\JoinTable(name="support_log_student",
* joinColumns={@ORM\JoinColumn(name="support_log_id",referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="student_id", referecedColumnName="id")}
* )
**/
private $student;
Now the symfony 2 Form should be
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('student', 'entity', array(
'class' => '<>\Entity\Student',
'property' => 'Name', //property you want to display on the select box
'label' => 'Belongs to Students',
'multiple' => true,
'constraints' => array(
new NotBlank(array('message' => 'Please choose atleast one student'))
)
))
....
;
}
On submit of form usually inside the Action
if ($editForm->isValid()) {
$entity = $editForm->getData();
$em->persist($entity); //this should take care of everything saving the manyToMany records
$em->flush();
return $this->redirect($this->generateUrl('support_log_edit', array('id' => $id)));
}
Please note: I haven't tested this code. I rewrote this code to fit the scenario mentioned in the question.