Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting FuelPHP:ORM排序方式有很多计数_Sorting_Orm_Fuelphp - Fatal编程技术网

Sorting 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

我有一个名为post的表,其中有许多评论我需要根据一篇文章中有多少评论对文章列表进行排序

在fuelPHP中,有没有在ORM中实现这一点的方法?在手动操作之前,我想确认一下


谢谢。

简短的回答是:不,没有。如果不手动执行,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将保持第一个数组的键顺序的特性