English 中文(简体)
理论2
原标题:doctrine2 many to many self referencing with intermediate details

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 DoctrineCommonCollectionsArrayCollection();
        $this->myFriends = new DoctrineCommonCollectionsArrayCollection();
    }

    // ...
}

然而,似乎你可以采用这种方法补充财产。

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
{
    // ...

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

    /**
     * @ORMManyToOne(targetEntity="User", inversedBy="links")
     * @ORMJoinColumn(name="user_id", referencedColumnName="id")
     * @var type
     */
    private $user;
}

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

我在研究停滞期时发现类似的问题,但更多涉及直接联系。

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

需要帮助了解许多自我参照代码:

问题回答

Solved it like this:

用户.php

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

用户Link.php

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

/**
 * @ORMManyToOne(targetEntity="User", inversedBy="relations")
 * @ORMJoinColumn(name="user_ud", referencedColumnName="id")
 */
protected $user;

/**
 * @ORMManyToOne(targetEntity="User")
 * @ORMJoinColumn(name="related_user_id", referencedColumnName="id")
 */
protected $relateduser;

它要求在控制器和模型中增加比值,但效果良好。 该系统使用户能够打电话:

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




相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签