Order by a count-column
Fri, 05/20/2011 - 18:49
Hi all!
In the code from examples:
<?php
$objProjectArray = Project::QueryArray(
QQ::All(),
QQ::Clause(
QQ::GroupBy(QQN::Project()->Id),
QQ::Count(QQN::Project()->PersonAsTeamMember->PersonId, 'team_member_count')
)
);
foreach ($objProjectArray as $objProject) {
_p($objProject->Name . ' (' . $objProject->GetVirtualAttribute('team_member_count') . ' team members)');
_p('<br/>', false);
}
?>is there any possibility to order the results by the virtual attribute 'team_member_count' ? Actually descending.
Thank You,
Sanyi.

I didn't actually try it in the example, but adding the following to the QQ:Clause list should work
QQ::OrderBy(QQ::Virtual("team_member_count"), false)Thank You, SP.
This is a step forward, but:
$objKepekArray = Favorits::QueryArray(QQ::All(),
QQ::Clause(
QQ::GroupBy(QQN::Favorits()->ModellId),
QQ::Count(QQN::Favorits()->ModellId, 'id_count'),
QQ::LimitInfo(100, $intNyiR),
QQ::OrderBy(QQ::Virtual("id_count"), false)
)
);
says:
Undefined Virtual Node: id_count .
What do I do wrong?
Thank You,
Sanyi.
I am sorry, looks like I mispoke. This has been a problem for a while. OrderBy does not recognize the QQuery computation virtual attributes. However, you can workaround this by using a SubSql instead. Its not clean, but it works.
So something like:
$objKepekArray = Favorits::QueryArray(
QQ::All(),
QQ::Clause(
QQ::Expand(
QQ::Virtual(
'id_count',
QQ::SubSql('SELECT count(modell_id) FROM favorits))
),
QQ::GroupBy(QQN::Favorits()->ModellId),
QQ::LimitInfo(100, $intNyiR),
QQ::OrderBy(QQ::Virtual("id_count"), false)
)
);
With a correction (sql command close ) this is working.
But the order is not what should be.
Unfortunately the qcubed does not read views, other way, the:
CREATE VIEW `favoritesview` AS select `favorits`.`modell_id` AS `modell_id`,count(`favorits`.`modell_id`) AS `megj` from `favorits` group by `favorits`.`modell_id` order by count(`favorits`.`modell_id`) descwould do perfectly.
Can we generate code for views, is there maybe a solution?
Sanyi.