<legend id='zyXnK'><style id='zyXnK'><dir id='zyXnK'><q id='zyXnK'></q></dir></style></legend>

    1. <i id='zyXnK'><tr id='zyXnK'><dt id='zyXnK'><q id='zyXnK'><span id='zyXnK'><b id='zyXnK'><form id='zyXnK'><ins id='zyXnK'></ins><ul id='zyXnK'></ul><sub id='zyXnK'></sub></form><legend id='zyXnK'></legend><bdo id='zyXnK'><pre id='zyXnK'><center id='zyXnK'></center></pre></bdo></b><th id='zyXnK'></th></span></q></dt></tr></i><div id='zyXnK'><tfoot id='zyXnK'></tfoot><dl id='zyXnK'><fieldset id='zyXnK'></fieldset></dl></div>
        <bdo id='zyXnK'></bdo><ul id='zyXnK'></ul>

      <small id='zyXnK'></small><noframes id='zyXnK'>

        <tfoot id='zyXnK'></tfoot>
      1. 需要帮助理解 Doctrine 多对多自引用代码

        Need help understanding Doctrine many to many self referencing code(需要帮助理解 Doctrine 多对多自引用代码)
          <legend id='xohV0'><style id='xohV0'><dir id='xohV0'><q id='xohV0'></q></dir></style></legend>
            <bdo id='xohV0'></bdo><ul id='xohV0'></ul>
            <i id='xohV0'><tr id='xohV0'><dt id='xohV0'><q id='xohV0'><span id='xohV0'><b id='xohV0'><form id='xohV0'><ins id='xohV0'></ins><ul id='xohV0'></ul><sub id='xohV0'></sub></form><legend id='xohV0'></legend><bdo id='xohV0'><pre id='xohV0'><center id='xohV0'></center></pre></bdo></b><th id='xohV0'></th></span></q></dt></tr></i><div id='xohV0'><tfoot id='xohV0'></tfoot><dl id='xohV0'><fieldset id='xohV0'></fieldset></dl></div>
            <tfoot id='xohV0'></tfoot>
              <tbody id='xohV0'></tbody>

              <small id='xohV0'></small><noframes id='xohV0'>

                1. 本文介绍了需要帮助理解 Doctrine 多对多自引用代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我无法从
                  (来源:

                  的解释

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

                  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

                  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.

                  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

                  # 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 'EntitiesUser' not found in D:ResourceLibraryFrameworksDoctrine oolssandboxindex.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

                  这篇关于需要帮助理解 Doctrine 多对多自引用代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Is PHP or PHP based web framework stateful or stateless?(PHP 或基于 PHP 的 Web 框架是有状态的还是无状态的?)
                  How to parse django style template tags(如何解析 django 样式模板标签)
                  What is a good setup for editing PHP in Emacs?(在 Emacs 中编辑 PHP 的好设置是什么?)
                  How to check whether specified PID is currently running without invoking ps from PHP?(如何在不从 PHP 调用 ps 的情况下检查指定的 PID 当前是否正在运行?)
                  What#39;s the difference between escapeshellarg and escapeshellcmd?(escapeshellarg 和escapeshellcmd 有什么区别?)
                  php in background exec() function(php 后台 exec() 函数)

                      <tbody id='drCw7'></tbody>

                        <small id='drCw7'></small><noframes id='drCw7'>

                        <tfoot id='drCw7'></tfoot>
                          • <bdo id='drCw7'></bdo><ul id='drCw7'></ul>

                          • <legend id='drCw7'><style id='drCw7'><dir id='drCw7'><q id='drCw7'></q></dir></style></legend>
                          • <i id='drCw7'><tr id='drCw7'><dt id='drCw7'><q id='drCw7'><span id='drCw7'><b id='drCw7'><form id='drCw7'><ins id='drCw7'></ins><ul id='drCw7'></ul><sub id='drCw7'></sub></form><legend id='drCw7'></legend><bdo id='drCw7'><pre id='drCw7'><center id='drCw7'></center></pre></bdo></b><th id='drCw7'></th></span></q></dt></tr></i><div id='drCw7'><tfoot id='drCw7'></tfoot><dl id='drCw7'><fieldset id='drCw7'></fieldset></dl></div>