Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9022839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:37:01+00:00 2026-06-16T05:37:01+00:00

I have a user which can have many friends. Means the relation is self-referencing.

  • 0

I have a user which can have many friends. Means the relation is self-referencing.
I now need the count of my friends friends according to their gender. I came up with the following solution so far.

My User Entity:

<?php

namespace Acme\UserBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity(repositoryClass="Acme\UserBundle\Entity\Repository\UserRepository")
 * @ORM\Table(name="users")
 */
class User extends BaseUser
{

    /**
     * @ORM\ManyToMany(targetEntity="Acme\UserBundle\Entity\User", inversedBy="friendsOf")
     * @ORM\JoinTable(name="map_user_friend",
     *      joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="friend_id", referencedColumnName="id")}
     *      )
     */
    protected $friends;

    /**
     * @ORM\ManyToMany(targetEntity="Acme\UserBundle\Entity\User", mappedBy="friends")
     */
    private $friendsOf;


    /**
     * @ORM\Column(type="string", length=1, nullable=true)
     */
    protected $gender;

    // other fields ...

}

My UserRepository has the following method:

public function getFriendsQuery(User $user)
{
    return $this->getEntityManager()->createQueryBuilder()
        ->select('u as user, COUNT(DISTINCT mf.id) as male_friends, COUNT(DISTINCT ff.id) as female_friends')
        ->from('Oneup\UserBundle\Entity\User', 'u')
        ->leftJoin('u.friendsOf', 'f')
        ->leftJoin('u.friendsOf', 'mf', 'with', 'mf.gender = :gender')->setParameter('gender', 'm')
        ->leftJoin('u.friendsOf', 'ff', 'with', 'ff.gender = :gender')->setParameter('gender', 'w')
        ->where('f = :user')->setParameter('user', $user)
        ->groupBy('u.id')
    ;
}

In the controller I now dump the SQL first by Doctrine\Common\Util\Debug::dump($qb->getQuery()->getSQL()); which returns

SELECT u0_.username AS username0,
       u0_.username_canonical AS username_canonical1,
       -- ... --
       u0_.gender AS gender19,
       -- ... --
       COUNT(DISTINCT u1_.id) AS sclr32,
       COUNT(DISTINCT u2_.id) AS sclr33
FROM users u0_
LEFT JOIN map_user_friend m4_ ON u0_.id = m4_.friend_id
LEFT JOIN users u3_ ON u3_.id = m4_.user_id
LEFT JOIN map_user_friend m5_ ON u0_.id = m5_.friend_id
LEFT JOIN users u1_ ON u1_.id = m5_.user_id
AND (u1_.gender = ?)
LEFT JOIN map_user_friend m6_ ON u0_.id = m6_.friend_id
LEFT JOIN users u2_ ON u2_.id = m6_.user_id
AND (u2_.gender = ?)
WHERE u3_.id = ?
GROUP BY u0_.id

Parameters are m for male, f for female and 21 for the current user. Now follows the weird behavior. Doctrine\Common\Util\Debug::dump($qb->getQuery()->execute()); returns me

array (size=2)
  0 =>
    array (size=3)
      'user' => string 'Oneup\UserBundle\Entity\User' (length=28)
      'male_friends' => string '2' (length=1)
      'female_friends' => string '2' (length=1)
  1 =>
    array (size=3)
      'user' => string 'Oneup\UserBundle\Entity\User' (length=28)
      'male_friends' => string '0' (length=1)
      'female_friends' => string '0' (length=1)

If I’m executing the query straight in mysql I get an other count:

+--------------+-----------------+-----------------+
| username0    | sclr32 (male)   | sclr33 (female) |
+--------------+-----------------+-----------------+
| foobar12     | 1               | 2               |
+--------------+-----------------+-----------------+
| foobar2      | 2               | 0               |
+--------------+-----------------+-----------------+

which is correct. Is this a doctrine bug, or am I just doing it plain wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:37:02+00:00Added an answer on June 16, 2026 at 5:37 am

    To answer my own question, the error was on the parameters in the Repository

       ->leftJoin('u.friendsOf', 'mf', 'with', 'mf.gender = :gender')->setParameter('gender', 'm')
       ->leftJoin('u.friendsOf', 'ff', 'with', 'ff.gender = :gender')->setParameter('gender', 'w')
    

    was wrong. doctrine takes the second value for both entries.

       ->leftJoin('u.friendsOf', 'mf', 'with', 'mf.gender = :male')->setParameter('male', 'm')
       ->leftJoin('u.friendsOf', 'ff', 'with', 'ff.gender = :female')->setParameter('female', 'w')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a situation in which user can single tap a control, which show
I have make a messaging system in which user can send messages to each
I have the following problem: I have a search box in which user can
I have a text-box into which a user can input a comment. The comment
I have a text editor in which a user can write HTML code. I
I have an NSView in which the user can draw circles. These circles are
I have the following select list from which the user can select multiple values.
I have a controller method called Edit in which the user can edit data
I have two buttons, which the user can click and will open a FileDiagloag
I have a UITableView with 3 empty rows. The user can select which text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.