2

I do have a query in a table called article.

$q = Doctrine_Query::create()
                    ->from('article a')
                    ->WhereIn('a.name', $input)
                    ->execute();

$input = array(
    0 => Tomato,
    1 => Apple,
    2 => Banana
); 

I find all inputs. But the output of the query is a different order: Apple, Banana, Tomato. I want the order like the input? How can I realize it?

TimWolla
  • 31,849
  • 8
  • 63
  • 96
craphunter
  • 961
  • 2
  • 13
  • 34
  • 1
    +1, interesting question... no idea though. I doubt you can do that in DQL. – greg0ire Feb 06 '12 at 21:46
  • 2
    Check out [this](https://groups.google.com/group/doctrine-user/browse_thread/thread/ffac5e58feac9ef9?pli=1) thread, it's not a solution, but a workaround. – Maerlyn Feb 06 '12 at 22:38

1 Answers1

2

I used something like this in a project that had MySQL as the RDBMS:

$ids = array(2,9,3,64,23,38); // example IDs
$qry->addOrderBy("FIND_IN_SET(q.id, '" . implode(",", $ids) . "')");

This was in a project where the values in $ids were known and trusted, and came from a Xapian search query which was ordered by relevance to the search term. Using FIND_IN_SET() enabled me to retrieve the right documents and present them in order of the relevance on the search results page.

Disclaimer: I don't know the level of support for this function, but it suited me fine at the time :-)

richsage
  • 26,912
  • 8
  • 58
  • 65