i am having trouble deciphering this block of code from doctrine documentation
/** @Entity */
class User
{
// ...
/**
* @ManyToMany(targetEntity="User", mappedBy="myFriends")
*/
private $friendsWithMe;
/**
* @ManyToMany(targetEntity="User", inversedBy="friendsWithMe")
* @JoinTable(name="friends",
* joinColumns={@JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="friend_user_id", referencedColumnName="id")}
* )
*/
private $myFriends;
// ...
}
below is how i decipher a one to many bidirectional relationship

(source: tumblr.com)
but if i use the same method, … below is what i get
alt text http://img514.imageshack.us/img514/2918/snagprogram0000.png
UPDATE
i shld clarify my question. basically, i dont understand how is the opposite of myFriends, friendsWithMe. how i shld make sense of this code and more importantly know how to code such relationships myself.
i give a try at answering my question, i am still quite blur with this, hope someone can really give a better answer,
so 1st to answer the question abt how do i derive with
$friendsWithMebasically, i started off with “decoding” a simpler, more common, many to many bidirectional relationship.
very straight forward. but how does this make sense in SQL?
code to implement
now to the actual model … in SQL
in code
ah ha! select users that are friends of given user. so this is how i get
$friendsWithMe. then to fill up theinversedBy&mappedBy& the rest of the class?1st look at the bottom note.
not clear without so much and deep thinking, abt 2 days. i guess
then as practice how do i create a many to many self referencing relationship from scratch?
the example i am going to work on is… hmm, quite crappy i think but, i’ll try 🙂 … 1 user/student can have many teachers. 1 teacher can have many users/students. 1 user can be a teacher and student here. u know like in forums such as these, when u answer someones questions, you are a teacher. when u ask, u are a student
the ERD will look like
some code to select, students of teachers, teachers of students
ok, the doctrine part?
which generated this tables for me
at last i done it! lets test it … erm i am getting
when i try to do a
zzz …
i have also blogged abt this question and my explaination on my tumblr