我的游戏模型中有一个方法,该方法可检索在一个数据库中的表中注册的所有游戏的列表,遍历找到的每个游戏,并从另一个数据库中获取数据,最后从数据库中获取随机图像URL,并将其追加到统计对象。感到困惑了吗?

不再感到困惑,这里是方法:

public static function getStats($player, $cache = 30) {

    // Return a collection of all games registered from the website database

    $games = Game::get(['game']);

    foreach($games as $game) {

        // For each iteration, return the statistics from the games database for that game


        // Grab game meta information from the website database, $game->game is the game name.

        $list = Game::where('game', $game->game)->remember($cache)->first();


        // From the game database, retrieve the statistics

        $stats[$game->game] = DB::connection('game')->table($list->stats_table)
                                                    ->select(DB::raw($list->statistics))
                                                    ->where('Username', $player)
                                                    ->remember($cache)
                                                    ->first();

        // Grab a random map image from the game database

        $stats[$game->game]['map'] = DB::connection('game')->table('Maps')
                                                           ->select('Image')
                                                           ->orderBy(DB::raw('RAND()'))
                                                           ->remember($cache)
                                                           ->first();


    }

    // Finally, return the statistics from the game paired with a random map image from the game

    return $stats;

}

我想将 map 查询附加到$ stats上,所以将其全部放在一个位置,当前该方法由于以下原因而失败:无法将stdClass类型的对象用作数组

如果没有 map ,则在$ stats上循环时,它是一系列游戏统计信息,键为游戏名称。

如果您仍然感到困惑(我不会怪您),请发表评论,我会做更多解释。

编辑:

这是我尝试使用->merge()的尝试:
public static function getStats($player, $cache = 30) {

    // Return a collection of all games registered from the website database

    $games = Game::get(['game']);

    foreach($games as $game) {

        // For each iteration, return the statistics from the games database for that game


        // Grab game meta information from the website database

        $list = Game::where('game', $game->game)->remember($cache)->first();


        // From the game database, retrieve the statistics

        $stats[$game->game] = DB::connection('game')->table($list->stats_table)
                                                    ->select(DB::raw($list->statistics))
                                                    ->where('Username', $player)
                                                    ->remember($cache)
                                                    ->first();

        // Grab a random map image from the game database

        $map = DB::connection('game')->table('Maps')
                                   ->select('Image')
                                   ->orderBy(DB::raw('RAND()'))
                                   ->remember($cache)
                                   ->first();

        $stats[$game->game] = $stats[$game->game]->merge($map);



    }

    // Finally, return the statistics from the game paired with a random map image from the game

    return $stats;
}

结果为调用运行Laravel 4.2的未定义方法stdClass::merge()

最佳答案

merge()
merge方法将给定的数组合并到原始集合中。如果给定数组中的字符串键与原始集合中的字符串键匹配,则给定数组的值将覆盖原始集合中的值

该方法的唯一参数是应合并的集合。这是一个例子。

<?php

// app/routes.php

Route::get('/', function()
{
    $a = Album::where('artist','Something Corporate')
        ->get();
    $b = Album::where('artist','Matt Nathanson')
        ->get();

    $result = $a->merge($b);

    $result->each(function($album)
    {
        echo $album->title.'<br />';
    });
});

别的
$array = array_merge($stats[$game->game]->toArray(), $map->toArray());
return Response::json($array);

关于php - Laravel::合并两个数据库查询对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25576134/

10-13 09:13
查看更多