Chris
Chris

Reputation: 2446

Mapping to columnA **or** columnB with Doctrine

I have a User Entity and a Relationship Entity. (The Relationship Entity is for identifying social relationships. facebook friends, twitter follower...etc)

Here is my relationship class to better understand it:

 /**
 * Describes a relationship between two users in the direction of user1 to user2. (i.e. user1 is 'friends' with user2)
 * This relationship may be mutual
 * @package AppBundle\Entity
 */
class Relationship {
    protected $id;
    protected $user1;
    protected $user2;
    protected $source;
    protected $type;
    protected $mutual;

    /**
     * @param string $source The source of the relationship information (facebook, twitter...etc.)
     * @param string $type The type of relationship. (source specific) e.g. friends, follower
     * @param int $user1 The user id
     * @param int $user2 The user id
     * @param bool $mutual If set to true than the reverse relationship is true (i.e. user2 is 'friends' with user1 as well)
     * This may seem obvious for facebook friends but the twitter follower relationship is not always mutual
     */
    function __construct($user1,$user2,$source,$type,$mutual)
    {
        $this->user1 = $user1;
        $this->user2 = $user2;
        $this->source = $source;
        $this->type = $type;
        $this->mutual = $mutual;

    }

The trouble I am having is mapping the User Entity to the Relationship entity.

A specific instance of a User might be user1 or user2 in the Relationship.

I can't map specifically to the Relationship Entity's user1 column because it might be user2

I was trying to avoid duplicate entries in the Relationships table by having one entry define the relationship bidirectionally (accomplished with the $mutual property).

Is there a way to map this in Doctrine such that the User would have access to their Relationships (e.g. User->getRelationships) regardless of whether they are user1 or user2?

Upvotes: 0

Views: 40

Answers (1)

Imran
Imran

Reputation: 4750

Honestly speaking "I don't know" :(

But the reason I am replying here is to bring a point. User1 and User2 both are instance of user. So, The problem here is that a user entity is acting as two different character. A user is referencing another user, I mean a User's primary key is users foreign key(self referencing). I personally faced this problem while making a social application. One user is a friend of another user. A user have a friend at the same time a user is a friend. I solved that problem by duplicating the row that you don't want to. By the way, I am also waiting for any batter solution. Best of luck

Upvotes: 1

Related Questions