我无法转换此查询:
SELECT c.title, COUNT(*),
(
SELECT ba_thumb.link
FROM ba_video
INNER JOIN video_channel ON video_channel.video_id=ba_video.id
INNER JOIN ba_thumb ON ba_thumb.video_id=video_channel.video_id
INNER JOIN ba_channel ON ba_channel.id=video_channel.channel_id
WHERE video_channel.channel_id=c.id
ORDER BY ba_video.views DESC, ba_thumb.id ASC
LIMIT 1
) AS ba_thumb_link
FROM ba_channel c
INNER JOIN video_channel ON video_channel.channel_id=c.id
INNER JOIN ba_video ON ba_video.id=video_channel.video_id
GROUP BY video_channel.channel_id
ORDER BY COUNT(*) DESC
进入DQL或使用QueryBuilder。
我在DQL中尝试过:
return $this->_em->createQuery('
SELECT c.title, COUNT(*),
(
SELECT t.link
FROM BAVideoGalleryBundle:Video v
INNER JOIN v.channels c
INNER JOIN v.thumbs t
WHERE c.id=mc.id
ORDER BY v.views DESC, t.id ASC
LIMIT 1
)
FROM BAVideoGalleryBundle:Channel mc
INNER JOIN BAVideoGalleryBundle:Video mv
GROUP BY mv.Channels.id
ORDER BY COUNT(*) DESC')
->getResult();
我明白了:
" [语法错误]第0行,第216行:错误:预期的Doctrine \ ORM \ Query \ Lexer :: T_CLOSE_PARENTHESIS,得到了限制' "
我尝试使用QueryBuilder:
$query = $this->getEntityManager()->createQueryBuilder()
->select('c.title, COUNT(*)')
->from('BAVideoGalleryBundle:Channel', 'mc')
->innerJoin('BAVideoGalleryBundle:Video', 'mv')
->groupBy('mv.Channels.id')
->orderBy('COUNT(*)', 'DESC');
$subquery = $this->getEntityManager()->createQueryBuilder()
->select('t.link')
->from('BAVideoGalleryBundle:Video', 'v')
->innerJoin('v.channels', 'c')
->innerJoin('v.thumbs', 't')
->where('c.id=mc.id')
->orderBy('v.views', 'DESC')
->orderBy('t.id', 'ASC')
->getQuery()
->getResult();
return $query->addSelect('('.$subquery->getDql().')')
但相关性不起作用,我得到:
[语义错误]第0行,第105行附近' mc.id ORDER BY':错误:' mc' 未定义
答案 0 :(得分:1)
这是Doctrine ORM可能会导致您解决的问题多于其中的一种情况。你可以:
ResultSetMapping
设置$this->_em
,这让我认为您处于EntityRepository
,因此您可以:$this->_em->getConnection()
获得DBAL connection然后只做$conn->query()
。显然,这样你就失去了ORM(数据库不可知等)的好处,但你可以认为ORM具有执行复杂查询的税务。我知道这些都不是理想的,但是从经验来看,有时候将Doctrine ORM推出去实现你所需要的更好。