title: Doctrine: How do i add elements to a relationship
body: <p>i think my question is not clear but i try to illustrate my point here. assuming i have a many to many, self referencing relationship where a user can be a teacher (say u post answers at SO) and a teacher can be a student (u may answer questions but may ask too) too. </p>
  
  <pre><code>namespace Entities;
  /** @Entity @Table(name="users")) */
  class User {
      /**
       * @Id @Column(type="integer")
       * @GeneratedValue(strategy="AUTO")
       */
      private $id;
      /**
       * @Column(type="string", length="30")
       */
      private $name;
      /**
       * @ManyToMany(targetEntity="User", inversedBy="teachers")
       * @JoinTable(name="Teachers_Students",
       *              joinColumns={@JoinColumn(name="teacher", referencedColumnName="id")},
       *              inverseJoinColumns={@JoinColumn(name="student", referencedColumnName="id")}
       *              )
       */
      private $students;
      /**
       * @ManyToMany(targetEntity="User", mappedBy="students")
       */
      private $teachers;
  
      function getName() {
        return $this-&gt;name;
      }
      function setName($name) {
        $this-&gt;name = $name;
      }
      function getStudents() {
        return $this-&gt;students;
      }
      function getTeachers() {
        return $this-&gt;teachers;
      }
  }
  </code></pre>
  
  <p>say i have a few users </p>
  
  <pre><code>$user1 = new User;
  $user1-&gt;setName("user 1");
  $user2 = new User;
  $user2-&gt;setName("user 2");
  $user3 = new User;
  $user3-&gt;setName("user 3");
  $user4 = new User;
  $user3-&gt;setName("user 4");
  </code></pre>
  
  <p>and i like to setup teacher-student relationships between them, i was reading up <a href="http://www.doctrine-project.org/projects/orm/2.0/docs/reference/working-with-objects/en#association-management-methods" rel="nofollow noreferrer">doctrine reference</a>, saw that u can use the <code>Collections::add()</code> to add elements to a collection</p>
  
  <pre><code>// user1 is a teacher to user2 &amp; 3
  $user1-&gt;getStudents()-&gt;add($user2);
  $user1-&gt;getStudents()-&gt;add($user3);
  
  // user2 is a teacher to user3
  $user2-&gt;getStudents()-&gt;add($user3);
  
  // user4 is a student to user2 
  // tests if adding something from the inverse side works
  $user4-&gt;getTeachers()-&gt;add($user2);
  </code></pre>
  
  <p>but this fails with </p>
  
  <blockquote>
    <p>Fatal error: Call to a member function
    add() on a non-object in
    D:\ResourceLibrary\Frameworks\Doctrine\tools\sandbox\index.php
    on line 70</p>
  </blockquote>
  
  <p>how can i add elements to a collection or a relationship?</p>