Sorting FuelPHP:ORM排序方式有很多计数
我有一个名为post的表,其中有许多评论我需要根据一篇文章中有多少评论对文章列表进行排序 在fuelPHP中,有没有在ORM中实现这一点的方法?在手动操作之前,我想确认一下Sorting FuelPHP:ORM排序方式有很多计数,sorting,orm,fuelphp,Sorting,Orm,Fuelphp,我有一个名为post的表,其中有许多评论我需要根据一篇文章中有多少评论对文章列表进行排序 在fuelPHP中,有没有在ORM中实现这一点的方法?在手动操作之前,我想确认一下 谢谢。简短的回答是:不,没有。如果不手动执行,ORM不可能为您这样排序。要解决此问题: // construct an array with post-id => comment-count $ordered = array(); foreach($posts as $id => $post) { $or
谢谢。简短的回答是:不,没有。如果不手动执行,ORM不可能为您这样排序。要解决此问题:
// construct an array with post-id => comment-count
$ordered = array();
foreach($posts as $id => $post)
{
$ordered[$id] = count($post->comments);
}
// order the array, largest count first
arsort($ordered, SORT_NUMERIC);
// merge the posts in, maintaining order
$ordered = array_replace($ordered, $posts);
这利用了array_replace将保持第一个数组的键顺序的特性