Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>i give a try at answering my question, i am still quite blur with this, hope someone can really give a better answer, </p> <p><strong>so 1st to answer the question abt how do i derive with <code>$friendsWithMe</code></strong></p> <p>basically, i started off with "decoding" a simpler, more common, many to many bidirectional relationship. </p> <ul> <li>1 user can be in many groups <ul> <li>$user->groups</li> </ul></li> <li>1 group can have many users <ul> <li>$group->users</li> </ul></li> </ul> <p>very straight forward. but how does this make sense in SQL? </p> <p><img src="https://farm5.static.flickr.com/4115/4814734734_7e25414308_b.jpg" alt="alt text"></p> <p>code to implement</p> <pre><code># select groups user is in select group_id from users_groups where user_id = 1 #select users of group select user_id from users_groups where group_id = 1 </code></pre> <p>now to the actual model ... in SQL</p> <p><img src="https://farm5.static.flickr.com/4121/4814728024_6a86f67930.jpg" alt="alt text"></p> <p>in code </p> <pre><code># select friends of given user # $user-&gt;myFriends select friend_id from friends where user_id = 1; # select users that are friends of given user # $user-&gt;friendsWithMe select user_id from friends where friend_id = 1; </code></pre> <p>ah ha! <strong>select users that are friends of given user</strong>. so this is how i get <code>$friendsWithMe</code>. then to fill up the <code>inversedBy</code> &amp; <code>mappedBy</code> &amp; the rest of the class?</p> <p>1st look at the bottom note. </p> <p><img src="https://farm5.static.flickr.com/4095/4814338717_a95748d80d_b.jpg" alt="alt text"></p> <p>not clear without so much and deep thinking, abt 2 days. i guess </p> <p><strong>then as practice how do i create a many to many self referencing relationship from scratch?</strong> </p> <p>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</p> <p>the ERD will look like </p> <p><img src="https://farm5.static.flickr.com/4136/4814407061_be7dd37d25.jpg" alt="alt text"></p> <p>some code to select, students of teachers, teachers of students</p> <pre><code># select students of teacher # $teacher-&gt;students select student from teacher_student where teacher = 1; # select teachers of student # $student-&gt;teachers select teacher from teacher_student where student = 2; </code></pre> <p>ok, the doctrine part? </p> <pre><code>/** @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; } </code></pre> <p>which generated this tables for me</p> <pre><code># users CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(30) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 #teachers_students CREATE TABLE `teachers_students` ( `teacher` int(11) NOT NULL, `student` int(11) NOT NULL, PRIMARY KEY (`teacher`,`student`), KEY `student` (`student`), CONSTRAINT `teachers_students_ibfk_2` FOREIGN KEY (`student`) REFERENCES `users` (`id`), CONSTRAINT `teachers_students_ibfk_1` FOREIGN KEY (`teacher`) REFERENCES `users` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 </code></pre> <p>at last i done it! lets test it ... erm i am getting </p> <blockquote> <p>Fatal error: Class 'Entities\User' not found in D:\ResourceLibrary\Frameworks\Doctrine\tools\sandbox\index.php on line 61</p> </blockquote> <p>when i try to do a </p> <pre><code>$user = new User; </code></pre> <p>zzz ... </p> <p>i have also blogged abt this question and my explaination on my <a href="http://tumblr.com/xfzdw66z1" rel="nofollow noreferrer">tumblr</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload