许多自我参考的中等细节 [英] doctrine2 many to many self referencing with intermediate details

查看:104
本文介绍了许多自我参考的中等细节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个自引用实体,但有一个关于关系的描述(可能还有其他信息)。一个例子就是社交网络应用中的朋友 - 人A(实体)可以与Person B(实体)链接,但是他们的关系可以被描述为朋友兄弟叔叔等。



从Doctrine文档中,一个类可以使用以下链接表进行自引用:

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

public function __construct(){
$ this-> friendsWithMe = new \Doctrine\Common\Collections\ArrayCollection();
$ this-> myFriends = new \Doctrine\Common\Collections\ArrayCollection();
}

// ...
}

然而,似乎您可以使用此方法添加属性。



问题是,是否有一种在单个类中处理这个问题的方法(即用户.php) - 或者实际上要做一个链接类 - 像UserLink.php?我开始关系时遇到了一个链接类:

  class UserLink 
{
// ...

/ **
* @ ORM\Column(type =text,nullable = true)
* /
protected $关系; //是兄弟,叔叔等

/ **
* @ ORM\ManyToOne(targetEntity =User,inversedBy =links)
* @ ORM\ JoinColumn(name =user_id,referencedColumnName =id)
* @var类型
* /
private $ user;
}

但我不知道接下来会发生什么?

研究stackoverflow我发现类似的问题,但更多关于直接链接。



Doctrine2:使用额外列处理多对多的最佳方式参考表格



需要帮助了解原则许多多个自引用代码

解决方案

已解决它像这样:



User.php

  / ** 
* @ ORM\OneToMany(targetEntity =UserLink,mappedBy =user)
* /
private $ relations;

UserLink.php

  / ** 
* @ ORM\Column(type =text,nullable = true)
* /
protected $ relationship;

/ **
* @ ORM\ManyToOne(targetEntity =User,inversedBy =relations)
* @ ORM\JoinColumn(name =user_ud ,referencedColumnName =id)
* /
protected $ user;

/ **
* @ ORM\ManyToOne(targetEntity =User)
* @ ORM\JoinColumn(name =related_user_id,referencedColumnName =id )
* /
protected $ relateduser;

它需要一些控制器和模型中的代码,但效果很好。它允许用户调用getRelations:

  {%for user.relations%} 
{{link。 relateduser.name}}({{link.relationship}})
{%endfor%}


I would like to create a self-referencing entity, but with a description (and possibly other information) about the relationship. An example of this would be friends in a social network application - Person A (entity) can link with Person B (entity), however their relationship could be described as "friend" "brother" "uncle" etc.

From the Doctrine documentation a class can be self referencing with a link table using the following:

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

    public function __construct() {
        $this->friendsWithMe = new \Doctrine\Common\Collections\ArrayCollection();
        $this->myFriends = new \Doctrine\Common\Collections\ArrayCollection();
    }

    // ...
}

however it doesn't seem that you can add properties using this method.

The question is, is there a way to handle this in a single class (i.e. User.php) - or would one actually have to make a "link" class - like UserLink.php? I kind of got stuck making a link class when I started on the relations:

class UserLink
{
    // ...

    /**
     * @ORM\Column(type="text", nullable=true)
     */
    protected $relationship; // being brother, uncle etc

    /**
     * @ORM\ManyToOne(targetEntity="User", inversedBy="links")
     * @ORM\JoinColumn(name="user_id", referencedColumnName="id")
     * @var type
     */
    private $user;
}

But I am not sure what happens next...?

Researching on stackoverflow I found similar questions, but more about direct linking.

Doctrine2: Best way to handle many-to-many with extra columns in reference table

Need help understanding Doctrine many to many self referencing code

解决方案

Solved it like this:

User.php

/**
 * @ORM\OneToMany(targetEntity="UserLink", mappedBy="user")
 */
private $relations;    

UserLink.php

/**
 * @ORM\Column(type="text", nullable=true)
 */
protected $relationship;

/**
 * @ORM\ManyToOne(targetEntity="User", inversedBy="relations")
 * @ORM\JoinColumn(name="user_ud", referencedColumnName="id")
 */
protected $user;

/**
 * @ORM\ManyToOne(targetEntity="User")
 * @ORM\JoinColumn(name="related_user_id", referencedColumnName="id")
 */
protected $relateduser;

It requires a bit more code in the controller and model, but works quite well. It enables the user to call getRelations:

{% for link in user.relations %}
    {{ link.relateduser.name }} ({{ link.relationship }})
{% endfor %}

这篇关于许多自我参考的中等细节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆