SQL query with UNION in Doctrine Symfony

 ̄綄美尐妖づ 提交于 2019-11-27 17:02:51

问题


I have a question about the translation of a SQL query in Doctrine Symfony. I would like to do a thing like that :

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 search all friends of the user 99.

My tables :

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

Precision : I would like to use the Symfony pager.

A solution ? Thank you !


回答1:


UNION is not supported within DQL, but you can issue your query using RAW SQL ->

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



回答2:


Other alternative to @ManseUK is:

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

return $statement->fetchAll();


来源:https://stackoverflow.com/questions/7981549/sql-query-with-union-in-doctrine-symfony

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!