Reputation: 119
Not sure how to do this: I've got some normal Doctrine2 entity with "enum" column. Each of enums got his own "rank" (which is their order to me), for example:
when I'm performing only select on this, everything works just fine. But I need to sort result on this enum column. So I tried to add to select():
$select = "(case "
. "when m.belt = 'blue' then 1 "
. "when m.belt = 'purple' then 2 "
. "when m.belt = 'brown' then 3 "
. "when m.belt = 'black' then 4 "
. "else 0 end) as beltN";
$query->addSelect($select);
$query->orderBy("beltN","ASC")
but obviously it will not work, cause entity has no "beltN" column and query returns me an array of arrays instead of objects. How to fix this to get collection of entities? I don't need to include beltN column into entity, just to have sorted collection. Thanks in advance!
Upvotes: 1
Views: 471
Reputation: 119
As a workaround (before Igor answered me) I added a method to fix another dimension of result array, cause I've been receiving two arrays instead of entity - one with entity and another with "beltN" number. It was doing just this:
public function fixAdditionalColumn(array &$members) {
if (count($members) == 0)
{
return;
}
if ($members[0] instanceof Member)
{
return;
}
foreach ($members as $i => $member) {
if ($member[0] instanceof Member)
{
$members[$i] = $member[0];
}
}
}
Not cool, but maybe someone will find it useful. Worked for me.
Upvotes: 0
Reputation: 9246
Try changing $select
to:
$select = "(case "
. "when m.belt = 'blue' then 1 "
. "when m.belt = 'purple' then 2 "
. "when m.belt = 'brown' then 3 "
. "when m.belt = 'black' then 4 "
. "else 0 end) as HIDDEN beltN";
It shoud return you an array of objects now.
Upvotes: 1