【问题标题】:Implementing a friends relationship in Symfony3 with Doctrine在 Symfony3 中使用 Doctrine 实现朋友关系
【发布时间】:2016-06-13 18:56:40
【问题描述】:

根据Implementing a friends list in Symfony2.1 with Doctrine的帖子, 我实现了@Roberto Trunfio 的解决方案。

/**
 * @ORM\ManyToMany(targetEntity="User", mappedBy="friends")
 */
private $friendsWithMe;

/**
 * @ORM\ManyToMany(targetEntity="User", inversedBy="friendsWithMe")
 * @ORM\JoinTable(name="friends",
 *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="friend_user_id", referencedColumnName="id")}
 *      )
 */
private $friends;

它可以工作,但是我想进一步添加额外的字段,例如“发送者、接收者、状态、发送日期...”,但我不知道如何集成它。有人可以帮我吗?谢谢

【问题讨论】:

    标签: doctrine-orm symfony


    【解决方案1】:

    如果你想用额外的属性来装饰关联,你需要一个关联类。 From the documentation(向下滚动一点):

    为什么多对多关联不太常见?因为您经常希望将附加属性与关联关联,在这种情况下您需要引入关联类。因此,直接的多对多关联消失了,取而代之的是三个参与类之间的一对多/多对一关联。

    您示例中的关联类是friendship。一个用户可以有很多朋友,一个用户可以是很多用户的朋友。或者更专业地说:一个用户有很多朋友,很多朋友都映射到一个朋友。在下面给出的示例中,Friendship 具有附加属性 $hasBeenHelpful(确实是不对称的)。

    // src/AppBundle/Entity/User.php
    /**
     * @ORM\Entity
     */
    class User
    {
        /**
         * @ORM\Column(type="integer")
         * @ORM\Id
         * @ORM\GeneratedValue(strategy="AUTO")
         */
        private $id;
    
        /**
         * @ORM\Column(type="string")
         */
        private $name;
    
        /**
         * The people who I think are my friends.
         *
         * @ORM\OneToMany(targetEntity="Friendship", mappedBy="user")
         */
        private $friends;
    
        /**
         * The people who think that I’m their friend.
         *
         * @ORM\OneToMany(targetEntity="Friendship", mappedBy="friend")
         */
        private $friendsWithMe;
    
        // …
    }
    

    还有友谊协会:

    // src/AppBundle/Entity/Friendship.php
    /**
     * @ORM\Entity
     */
    class Friendship
    {
        /**
         * @ORM\ManyToOne(targetEntity="User", inversedBy="friends")
         * @ORM\Id
         */
        private $user;
    
        /**
         * @ORM\ManyToOne(targetEntity="User", inversedBy="friendsWithMe")
         * @ORM\Id
         */
        private $friend;
    
        /**
         * Example of an additional attribute.
         *
         * @ORM\Column(type="boolean")
         */
        private $hasBeenHelpful;
    
        // …
    }
    

    您可能想在 User 类中添加一些函数,例如

    <?php
    
    use Doctrine\Common\Collections\ArrayCollection;
    
    class User
    {
        public function __construct()
        {
            $this->friends = new ArrayCollection();
            $this->friendsWithMe = new ArrayCollection();
        }
    
        public function addFriendship(Friendship $friendship)
        {
            $this->friends->add($friendship);
            $friendship->friend->addFriendshipWithMe($friendship);
        }
    
        public function addFriendshipWithMe(Friendship $friendship)
        {
            $this->friendsWithMe->add($friendship);
        }
    
        public function addFriend(User $friend)
        {
            $fs = new Friendship();
            $fs->setUser($this);
            $fs->setFriend($friend);
            // set defaults
            $fs->setHasBeenUseful(true);
    
            $this->addFriendship($fs);
        }
    }
    

    【讨论】:

    • 您好,感谢您的回答。在我添加朋友的函数中,我遇到了这个错误:类型错误:传递给 AppBundle\Entity\User::addFriend() 的参数 1 必须是 AppBundle\Entity\Friendship 的实例,给定的 AppBundle\Entity\User 的实例,在第 35 行调用 /Applications/MAMP/htdocs/equilike/src/AppBundle/Controller/Member/FriendController.php 你知道我该如何解决吗? (我的友情管理代码太长,评论里不加)
    • 显然,您的 FriendController 调用了$user-&gt;addFriend($otherUser),但签名是addFriend(Friendship $fs)。您可以将签名更改为addFriend(User $friend),并在此函数内创建一个具有默认属性的友谊对象。您还应该添加一个函数addFriendship(Friendship $fs)
    • 嗨,谢谢。我的添加功能现在正在工作。但是我认为它的工作方式类似于“关注”功能(如 Twitter),而不是“友谊”(如 Facebook)。我错了吗 ?还是我应该重新考虑我的实体协会的组织?你需要更多关于我想要做什么的信息吗?感谢您的帮助
    • @barmatt 我认为这些考虑超出了原始问题的范围。您可能想打开另一个问题,询问各种类似友谊的关系的差异(而不是如何实现它们)。另外,如果您发现我的回答有帮助,请考虑对其进行投票。谢谢!
    • 好的,我明白了。我要创建一个新帖子。感谢您的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-05
    • 1970-01-01
    • 1970-01-01
    • 2013-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多