<legend id='8FV3V'><style id='8FV3V'><dir id='8FV3V'><q id='8FV3V'></q></dir></style></legend>

  • <small id='8FV3V'></small><noframes id='8FV3V'>

  • <tfoot id='8FV3V'></tfoot>

      <i id='8FV3V'><tr id='8FV3V'><dt id='8FV3V'><q id='8FV3V'><span id='8FV3V'><b id='8FV3V'><form id='8FV3V'><ins id='8FV3V'></ins><ul id='8FV3V'></ul><sub id='8FV3V'></sub></form><legend id='8FV3V'></legend><bdo id='8FV3V'><pre id='8FV3V'><center id='8FV3V'></center></pre></bdo></b><th id='8FV3V'></th></span></q></dt></tr></i><div id='8FV3V'><tfoot id='8FV3V'></tfoot><dl id='8FV3V'><fieldset id='8FV3V'></fieldset></dl></div>
        <bdo id='8FV3V'></bdo><ul id='8FV3V'></ul>
      1. 学说的多对多自参照和互惠

        Doctrine#39;s Many-To-Many Self-Referencing and reciprocity(学说的多对多自参照和互惠)
        <tfoot id='k0ljV'></tfoot>
      2. <small id='k0ljV'></small><noframes id='k0ljV'>

          <tbody id='k0ljV'></tbody>

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

                <bdo id='k0ljV'></bdo><ul id='k0ljV'></ul>
                  <legend id='k0ljV'><style id='k0ljV'><dir id='k0ljV'><q id='k0ljV'></q></dir></style></legend>
                • 本文介绍了学说的多对多自参照和互惠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  默认情况下,Doctrine 下的自引用 ManyToMany 关系涉及拥有方和反向方,如 文档.

                  By default, self-referencing ManyToMany relationships under Doctrine involve an owning side and an inverse side, as explained in the documentation.

                  有没有办法实现双方之间没有差异的互惠关联?

                  Is there a way to implement a reciprocal association whithout difference between both sides?

                  按照文档中的示例:

                  <?php
                  /** @Entity **/
                  class User
                  {
                      // ...
                  
                      /**
                       * @ManyToMany(targetEntity="User")
                       **/
                      private $friends;
                  
                      public function __construct() {
                          $this->friends = new DoctrineCommonCollectionsArrayCollection();
                      }
                  
                      // ...
                  }
                  

                  因此,将 entity1 添加到 entity2friends 意味着 entity2 将在 entity1的朋友.

                  So, adding entity1 to entity2s friends implies that entity2 will be in entity1s friends.

                  推荐答案

                  解决这个问题的方法有很多种,都取决于对朋友"关系的要求.

                  There are a number of ways to solve this problem, all depending on what the requirements for the "friends" relation are.

                  单向

                  一种简单的方法是使用单向多对多关联,并将其视为双向关联(保持双方同步):

                  A simple approach would be to use a unidirectional ManyToMany association, and treat it as if it where a bidirectional one (keeping both sides in sync):

                  /**
                   * @Entity
                   */
                  class User
                  {
                      /**
                       * @Id
                       * @Column(type="integer")
                       */
                      private $id;
                  
                      /**
                       * @ManyToMany(targetEntity="User")
                       * @JoinTable(name="friends",
                       *     joinColumns={@JoinColumn(name="user_a_id", referencedColumnName="id")},
                       *     inverseJoinColumns={@JoinColumn(name="user_b_id", referencedColumnName="id")}
                       * )
                       * @var DoctrineCommonCollectionsArrayCollection
                       */
                      private $friends;
                  
                      /**
                       * Constructor.
                       */
                      public function __construct()
                      {
                          $this->friends = new DoctrineCommonCollectionsArrayCollection();
                      }
                  
                      /**
                       * @return array
                       */
                      public function getFriends()
                      {
                          return $this->friends->toArray();
                      }
                  
                      /**
                       * @param  User $user
                       * @return void
                       */
                      public function addFriend(User $user)
                      {
                          if (!$this->friends->contains($user)) {
                              $this->friends->add($user);
                              $user->addFriend($this);
                          }
                      }
                  
                      /**
                       * @param  User $user
                       * @return void
                       */
                      public function removeFriend(User $user)
                      {
                          if ($this->friends->contains($user)) {
                              $this->friends->removeElement($user);
                              $user->removeFriend($this);
                          }
                      }
                  
                      // ...
                  
                  }
                  

                  当你调用$userA->addFriend($userB)时,$userB会被添加到$userA,并且 $userA 将被添加到 $userB 中的朋友集合中.

                  When you call $userA->addFriend($userB), $userB will be added to the friends-collection in $userA, and $userA will be added to the friends-collection in $userB.

                  这也会导致 2 条记录添加到朋友"表中(1,2 和 2,1).虽然这可以看作是重复数据,但它会大大简化您的代码.例如,当您需要查找 $userA 的所有好友时,您可以简单地执行:

                  It will also result in 2 records added to the "friends" table (1,2 and 2,1). While this can be seen as duplicate data, it will simplify your code a lot. For example when you need to find all friends of $userA, you can simply do:

                  SELECT u FROM User u JOIN u.friends f WHERE f.id = :userId
                  

                  无需像双向关联那样检查 2 个不同的属性.

                  No need to check 2 different properties as you would with a bidirectional association.

                  双向

                  当使用双向关联时,User 实体将有 2 个属性,例如 $myFriends$friendsWithMe.您可以按照上述相同的方式使它们保持同步.

                  When using a bidirectional association the User entity will have 2 properties, $myFriends and $friendsWithMe for example. You can keep them in sync the same way as described above.

                  主要区别在于,在数据库级别,您将只有一条记录表示关系(1,2 或 2,1).这使得查找所有朋友"查询变得更加复杂,因为您必须检查这两个属性.

                  The main difference is that on a database level you'll only have one record representing the relationship (either 1,2 or 2,1). This makes "find all friends" queries a bit more complex because you'll have to check both properties.

                  您当然可以通过确保 addFriend() 将同时更新 $myFriends$friendsWithMe 来使用数据库中的 2 条记录(并保持对方同步).这将增加您的实体的一些复杂性,但查询变得不那么复杂.

                  You could of course still use 2 records in the database by making sure addFriend() will update both $myFriends and $friendsWithMe (and keep the other side in sync). This will add some complexity in your entities, but queries become a little less complex.

                  单对多/多对一

                  如果您需要一个用户可以添加朋友的系统,但该朋友必须确认他们确实是朋友,您需要将该确认存储在联接表中.然后,您不再有 ManyToMany 关联,而是类似于 User <- OneToMany -> Friendship <- ManyToOne -> User.

                  If you need a system where a user can add a friend, but that friend has to confirm that they are indeed friends, you'll need to store that confirmation in the join-table. You then no longer have a ManyToMany association, but something like User <- OneToMany -> Friendship <- ManyToOne -> User.

                  你可以阅读我关于这个主题的博文:

                  You can read my blog-posts on this subject:

                  • 学说 2: 如何处理带有额外列的连接表
                  • 详细了解教义 2 中的一对多/多对一关联

                  这篇关于学说的多对多自参照和互惠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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='dNoPg'></tbody>

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

                      <bdo id='dNoPg'></bdo><ul id='dNoPg'></ul>

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

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

                          • <tfoot id='dNoPg'></tfoot>