2011-03-30 52 views
0

如何在教義2中訂購?我甚至不能做一個近似..教義2,我該如何命令這個?

表:

book 
---- 
id 
title 

vote 
---- 
id 
book_id 
value 

author 
---- 
id 
name 

book_author 
---- 
book_id 
author_id 

什麼我是從作者的ID,所以我要回報以票訂的書。訂購像這樣的ORDER BY(SUM(v.value)/ COUNT(v))和GROUP BY v.book_id ..但我不知道如何做表之間的所有連接..例如一個簡單的連接返回從作者的圖書將是這樣的:

SELECT b。從實體\書b JOIN b.authors一個WHERE a.id = {$作者 - > ID}

..但我怎樣才能把在這裏投票?一些建議?

乾杯

+0

使這種排序部分的實體之間的默認關聯? – Tjorriemorrie 2011-05-14 08:13:16

回答

1

嗯.. finnally我解決了做一個這樣的SQL:

$sql = "SELECT b2.*, b1.mark FROM (SELECT book_id, SUM(v.value)/COUNT(*) as mark FROM vote v GROUP BY v.book_id) as b1" . 
        " RIGHT JOIN" . 
        " (SELECT * FROM author_book ba JOIN book b ON ba.book_id=b.id WHERE ba.author_id = {$author->id}) as b2 ON b2.id=b1.book_id ORDER BY b1.mark DESC"; 

        $rsm = new \Doctrine\ORM\Query\ResultSetMapping; 
        $rsm->addEntityResult('Project\Entity\Book', 'b'); 
        $rsm->addFieldResult('b', 'id', 'id'); 
        $rsm->addFieldResult('b', 'isbn', 'isbn'); 
        $rsm->addFieldResult('b', 'title', 'title'); 
        $rsm->addFieldResult('b', 'summary', 'summary'); 
        $rsm->addFieldResult('b', 'pages', 'pages'); 
        $rsm->addFieldResult('b', 'price', 'price'); 
        $rsm->addFieldResult('b', 'published_at', 'published_at'); 

也許將是有用的人,歡呼聲。