Is this the proper way to handle an ordered array with Doctrine2's WHERE IN expression?

If it makes you feel better, you can use the ExpressionBuilder $ex = $em->getExpressionBuilder(); $dql = 'SELECT be FROM BlogPost be WHERE ' . $ex->in('b. Id', $ids)); $query = $em->createQuery($dql); function cmp($a, $b) { global $ids; return (array_search($a->getId(), $ids) getId(), $ids))?

-1 : 1; } usort($res, 'cmp') It's a bit cleaner, but does the same as you behind the screens.

If it makes you feel better, you can use the ExpressionBuilder. $ex = $em->getExpressionBuilder(); $dql = 'SELECT be FROM BlogPost be WHERE ' . $ex->in('b.Id', $ids)); $query = $em->createQuery($dql); function cmp($a, $b) { global $ids; return (array_search($a->getId(), $ids) getId(), $ids))?

-1 : 1; } usort($res, 'cmp'); It's a bit cleaner, but does the same as you behind the screens.

Pretty nice solution! – tom Apr 15 at 9:21 Thanks, I do like that better. Any idea for preserving ID order with DQL?

– Steve Apr 16 at 16:23 no, I don't think that is possible. You either have to read them one by one, in the sequence you want to have them, or sort them afterwards. (I edited my original answer with an example) – tvlooy Apr 18 at 13:51 I'm pretty sure now that it's not possible.

You can do the following in SQL: "select * from table order by (case id when 3 then 0 when 1 then 1 when 4 then 2 end)" But CaseExpression not yet supported in Doctrine2, it doesn't work. – tvlooy Apr 18 at 14:15.

You should pas the $ids array through setParameter() function as this is a best practice in doctrine: $query = $this->_em->createQuery('SELECT be FROM BlogPost WHERE b. Id IN (?1)'); $query->setParameter(1, implode(',', $ids)); I think the IN statement will not preserve the order of the passed ID's as IN will match the first found $id not depending on the order.

1 That only returns a single result. – Steve Apr 15 at 3:04 I just did test it and you're correct, it only returns one value. Sadly I want to report this as a bug but the jira is down.

– tom Apr 15 at 5:53.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions