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?
To answer my own question, the error was on the parameters in the Repository
was wrong. doctrine takes the second value for both entries.