tags:

views:

34

answers:

1

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.

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->name;
    }
    function setName($name) {
      $this->name = $name;
    }
    function getStudents() {
      return $this->students;
    }
    function getTeachers() {
      return $this->teachers;
    }
}

say i have a few users

$user1 = new User;
$user1->setName("user 1");
$user2 = new User;
$user2->setName("user 2");
$user3 = new User;
$user3->setName("user 3");
$user4 = new User;
$user3->setName("user 4");

and i like to setup teacher-student relationships between them, i was reading up doctrine reference, saw that u can use the Collections::add() to add elements to a collection

// user1 is a teacher to user2 & 3
$user1->getStudents()->add($user2);
$user1->getStudents()->add($user3);

// user2 is a teacher to user3
$user2->getStudents()->add($user3);

// user4 is a student to user2 
// tests if adding something from the inverse side works
$user4->getTeachers()->add($user2);

but this fails with

Fatal error: Call to a member function add() on a non-object in D:\ResourceLibrary\Frameworks\Doctrine\tools\sandbox\index.php on line 70

how can i add elements to a collection or a relationship?

+1  A: 

Remember that your collection variables are just regular ol' class properties. Which means they'll be null until you initialize them. The typical thing to do is instantiate them using Doctrine's ArrayCollection class, which will allow you to use the methods you described.

Try this:

public function __construct()
{
   $this->students = new \Doctrine\Common\Collections\ArrayCollection();
   $this->teachers = new \Doctrine\Common\Collections\ArrayCollection();

}
Bryan M.
thanks, that fixes it. but must i do that for all relationships? the sample code in the docs don't specify that i must initialize a relationship like this. it only said to use the `@OneToMany`, `@ManyToMany`, etc. annotations
jiewmeng
It is recommended by Doctrine under 'best practices' http://www.doctrine-project.org/projects/orm/2.0/docs/reference/best-practices/en#initialize-collections-in-the-constructor It is not required, but without it, you could not rely on those properties to be non-null until Doctrine passively initialized them through hydration.
Bryan M.