PHPLithiumを使用して埋め込まれたMongoDBオブジェクトを並べ替えたいと思います。ほぼ次のようなモデル「スレッド」があります。
{
"_id": ObjectId("4f71bf4618b602580f000009"),
"postings": [
{text: "a", upvotes: 15, /*...*/},
{text: "b", upvotes: 23},
{text: "c", upvotes: 16},
{text: "d", upvotes: 42}
],
// bla
}
それでは、賛成票に応じて投稿を並べ替えたいと思います。私はすでに私が望むことを大まかに行うメソッドを書きました:
public function postings_sort_by_upvotes($thread) {
$postings = $thread->postings->to('array');
usort($postings, function($a, $b) {
return ($a['upvotes'] > $b['upvotes'] ? 1 : -1);
});
return $postings;
}
lithium\data\collection\DocumentArray
これは機能しますが、ソートされていない投稿はタイプであるのに対し、明らかにそれは投稿をプレーン配列として返します。
オブジェクトではなく配列で本当に苦労する必要がありますか、それとも元のデータ型を失うことなく配列を並べ替えることができる方法はありますか?