title: Symfony 2 add relationship on existing entities (courses & courses_students & students)
body: <p>I am new to symfony framework. I would like to ask you guys how to add relationship on existing entities. I have <code>students</code>, <code>courses</code> and <code>courses_students</code> tables. In students table there are 3 students and in courses table there are 2 courses. In below code I am using findAll() function. But in my development I am passing array from twig template. </p>
  
  <ol>
  <li>How can I add records in courses_students table using ajax?</li>
  <li><p>How can I remove records in courses_students table using ajax?</p>
  
  <pre><code>public function amcsAction(Request $request){
      if($request-&gt;isXmlHttpRequest()){
          $em = $this-&gt;getDoctrine()-&gt;getManager();
          $course = $em-&gt;getRepository('FicusEschoolBundle:Course')-&gt;find($request-&gt;get('cid'));
  
          if (!$course) {
              throw $this-&gt;createNotFoundException('Unable to find Course entity.');
          }
  
          $students = $em-&gt;getRepository('FicusEschoolBundle:Student')-&gt;findAll();
  
          $course-&gt;addStudent($students);
          $em-&gt;persist($course);
          $em-&gt;flush();
  
      }
  }
  </code></pre></li>
  </ol>
  
  <p><strong>UPDATED</strong>: Info about Entity</p>
  
  <p>Course Table</p>
  
  <pre><code> private $teacher;
  
  /**
   * @ORM\ManyToOne(targetEntity="Subject", inversedBy="courses")
   * @ORM\JoinColumn(name="subject_id", referencedColumnName="id")
   */
  private $subject;
  
  /**
   * @ORM\ManyToOne(targetEntity="Coursetype", inversedBy="courses")
   * @ORM\JoinColumn(name="coursetype_id", referencedColumnName="id")
   */
  private $coursetype;
  
  /**
   * @ORM\ManyToOne(targetEntity="Schoolclass", inversedBy="courses")
   * @ORM\JoinColumn(name="schoolclass_id", referencedColumnName="id")
   */
  private $schoolclass;
  
  /**
   * @ORM\ManyToMany(targetEntity="Student", mappedBy="courses")
   */
  private $students;
  </code></pre>
  
  <p>Student Table</p>
  
  <pre><code>/**
   * @ORM\Column(name="enrolledDate", type="datetime")
   */
  private $enrolledDate;
  
   /**
   * @ORM\ManyToMany(targetEntity="Course")
   * @ORM\JoinTable(name="courses_students", 
   *      joinColumns={@ORM\JoinColumn(name="student_id", referencedColumnName="id")},
   *      inverseJoinColumns={@ORM\JoinColumn(name="course_id", referencedColumnName="id")}
   * )
   */
  private $courses;
  
  
  /**
   * @ORM\OneToOne(targetEntity="Studentprofile", mappedBy="student")
   */
  private $studentProfile;
  </code></pre>
  
  <p>Jquery Function</p>
  
  <pre><code>    function mcs() {
          sids = [];
          $('#selectedStudents option').each(function (i, selected) {
              sids.push($(selected).val());
          });
  
          $.ajax({
              url: "{{ path('amcs') }}",
              type: "POST",
              dataType: "json",
              async: false,
              data: {"cid": _cid, "students": sids}
          }).done(function (data) {
              console.log(data);
          });
      }
  </code></pre>