Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 938593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:40:10+00:00 2026-05-15T21:40:10+00:00

i am having trouble deciphering this block of code from doctrine documentation /** @Entity

  • 0

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

alt text
(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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T21:40:10+00:00Added an answer on May 15, 2026 at 9:40 pm

    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 $friendsWithMe

    basically, i started off with “decoding” a simpler, more common, many to many bidirectional relationship.

    • 1 user can be in many groups
      • $user->groups
    • 1 group can have many users
      • $group->users

    very straight forward. but how does this make sense in SQL?

    alt text

    code to implement

    # 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
    

    now to the actual model … in SQL

    alt text

    in code

    # select friends of given user
    # $user->myFriends
    select friend_id from friends
    where user_id = 1;
    
    # select users that are friends of given user
    # $user->friendsWithMe
    select user_id from friends
    where friend_id = 1;
    

    ah ha! select users that are friends of given user. so this is how i get $friendsWithMe. then to fill up the inversedBy & mappedBy & the rest of the class?

    1st look at the bottom note.

    alt text

    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

    alt text

    some code to select, students of teachers, teachers of students

    # select students of teacher
    # $teacher->students
    select student from teacher_student 
    where teacher = 1;
    
    # select teachers of student
    # $student->teachers
    select teacher from teacher_student
    where student = 2;
    

    ok, the doctrine part?

    /** @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;
    }
    

    which generated this tables for me

    # 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
    

    at last i done it! lets test it … erm i am getting

    Fatal error: Class ‘Entities\User’ not
    found in
    D:\ResourceLibrary\Frameworks\Doctrine\tools\sandbox\index.php
    on line 61

    when i try to do a

    $user = new User;
    

    zzz …

    i have also blogged abt this question and my explaination on my tumblr

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble deciphering a passing argument ... from distinct Objective-C type warning.
Having trouble linking the Stomp.framework into an iPhone SDK application. http://code.google.com/p/stompframework/ I follow the
I'm trying to benchmark a simple webserver I wrote, but I'm having trouble deciphering
having trouble coming up with search parameters for this issue, so I can't find
Having trouble with this jQuery function: $(#content).siblings().each(function(i){ heightOfSiblings = heightOfSiblings + this.outerHeight(); }); Error
Having trouble with the following segment of code. I'm getting a parameter count mismatch.
Having trouble with an HQL query. If I remove the avg(..) from it it
im having trouble extracting the trend name and search query from the json response
I am having trouble with a camera class I am trying to use in
Having trouble finding a solution for my situation here. Sorry if this has been

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.