SQL query with UNION in Doctrine Symfony

I have a question about translating an SQL query into Doctrine Symfony. I would like to do the following:

SELECT m.*
FROM member m
INNER JOIN (
  SELECT id_member
  FROM friend
  WHERE id_friend=99
  UNION
  SELECT id_friend
  FROM friend
  WHERE id_member=99
) a ON m.id=a.id_member 
WHERE m.visible=1

In this example, I am looking for all friends of user 99.

My tables:

Member: (id, name, visible)
Friend: (id, id_member, id_friend, active)

Accuracy: I would like to use the Symfony pager.

Decision? Thank!

+5
source share
2 answers

UNION is not supported in DQL, but you can send a query using RAW SQL ->

$q = Doctrine_Manager::getInstance()->getCurrentConnection();
$result = $q->execute(" -- RAW SQL HERE -- ");
+4
source

Another alternative to @ManseUK:

$em = $this->getEntityManager();
$connection = $em->getConnection();
$statement = $connection->prepare("-- RAW SQL HERE --");
$statement->execute();

return $statement->fetchAll();
+2
source

All Articles