0
votes

I have Many-To-Many Self-referencing relation in my user entity.

// Acme\DemoBundle\Resources\config\doctrine\User.orm.yml
Acme\DemoBundle\Entity\User:
    type: entity
    repositoryClass: Acme\DemoBundle\Entity\Repository\UserRepository
    table: users
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        username:
            type: string
            length: 25
            unique: true
    manyToMany:
        friendsWithMe:
            targetEntity: User
            mappedBy: myFriends
        myFriends:
            targetEntity: User
            inversedBy: friendsWithMe
            joinTable:
                name: friends
                joinColumns:
                    user_id:
                        referencedColumnName: id
                inverseJoinColumns:
                    friend_user_id:
                        referencedColumnName: id

Now i want to get three different user collections:

  1. MyFriends - collection of entities with myFriend == true and friendWithMe == false
  2. FriendWithMe - collection of users with myFriend == false and friendWithMe == true
  3. MutualFriends - collection of users with myFriend == true and friendWithMe == true

Standart getMyFriends an getFriendsWithMe (generated in user entity) returns all MyFriends and FriendWithMe records if friends are mutual =(

I tried to dig into the side of the Criteria but it does not work with many-to-many relations.

1

1 Answers

1
votes

I think there is a general problem in your design structure. Self-Referencing relations in Doctrine are equal to Mutual relations. The states, that not both users are friends with each others, sounds more like a FriendsRequest. Maybe you should change this behavior to have on relation for MutualFriends and two different relations for MyFriendRequests and RecievedFriendRequest.

Antother possiblity is to use an Relationship Entity like "Friendship" this could look like

// Acme\DemoBundle\Resources\config\doctrine\Friendship.orm.yml
Acme\DemoBundle\Entity\Friendship:
    type: entity
    repositoryClass: Acme\DemoBundle\Entity\Repository\FriendshipRepository
    table: friendship
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        user_one_accepted:
            type: boolean
        user_two_accepted:
            type: boolean
    manyToOne:
        user_one:
            targetEntity: User
        user_two:
            targetEntity: User