0

How can I order this in doctrine 2? I can't even do an approximation..

tables:

book
----
id
title

vote
----
id
book_id
value

author
----
id
name

book_author
----
book_id
author_id

What I have is an id from author, so I have to return the books ordered by votes. Ordering something like this ORDER BY (SUM(v.value) / COUNT(v)) and GROUP BY v.book_id .. but I have no idea how to do all the connections between the tables.. for example a simple connection for return the books from an author will be something like this:

SELECT b FROM Entity\Book b JOIN b.authors a WHERE a.id = {$author->id}

.. but how I can put votes here? Some suggestions?

Cheers

1
  • Make that ordering part of the default assocation between the entities? May 14, 2011 at 8:13

1 Answer 1

1

Well.. finnally I solved doing a sql like this:

$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');

Maybe will be useful for someone, cheers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.