我的游戏模型中有一个方法可以检索在一个数据库的表中注册的所有游戏的列表,遍历找到的每个游戏并从另一个数据库获取数据,最后随机抓取一个从数据库中提取图像 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;
}
这导致 调用未定义的方法 stdClass::merge() 运行 Laravel 4.2。
最佳答案
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/
总的来说,我对ruby还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用
在控制台中反复尝试之后,我想到了这种方法,可以按发生日期对类似activerecord的(Mongoid)对象进行分组。我不确定这是完成此任务的最佳方法,但它确实有效。有没有人有更好的建议,或者这是一个很好的方法?#eventsisanarrayofactiverecord-likeobjectsthatincludeatimeattributeevents.map{|event|#converteventsarrayintoanarrayofhasheswiththedayofthemonthandtheevent{:number=>event.time.day,:event=>ev
我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.
我主要使用Ruby来执行此操作,但到目前为止我的攻击计划如下:使用gemsrdf、rdf-rdfa和rdf-microdata或mida来解析给定任何URI的数据。我认为最好映射到像schema.org这样的统一模式,例如使用这个yaml文件,它试图描述数据词汇表和opengraph到schema.org之间的转换:#SchemaXtoschema.orgconversion#data-vocabularyDV:name:namestreet-address:streetAddressregion:addressRegionlocality:addressLocalityphoto:i
exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby中使用两个参数异步运行exe吗?我已经尝试过ruby命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何rubygems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除
我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss
好的,所以我的目标是轻松地将一些数据保存到磁盘以备后用。您如何简单地写入然后读取一个对象?所以如果我有一个简单的类classCattr_accessor:a,:bdefinitialize(a,b)@a,@b=a,bendend所以如果我从中非常快地制作一个objobj=C.new("foo","bar")#justgaveitsomerandomvalues然后我可以把它变成一个kindaidstring=obj.to_s#whichreturns""我终于可以将此字符串打印到文件或其他内容中。我的问题是,我该如何再次将这个id变回一个对象?我知道我可以自己挑选信息并制作一个接受该信
如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象
我在Rails工作并有以下类(class):classPlayer当我运行时bundleexecrailsconsole然后尝试:a=Player.new("me",5.0,"UCLA")我回来了:=>#我不知道为什么Player对象不会在这里初始化。关于可能导致此问题的操作/解释的任何建议?谢谢,马里奥格 最佳答案 havenoideawhythePlayerobjectwouldn'tbeinitializedhere它没有初始化很简单,因为你还没有初始化它!您已经覆盖了ActiveRecord::Base初始化方法,但您没有调
我有一个这样的哈希数组:[{:foo=>2,:date=>Sat,01Sep2014},{:foo2=>2,:date=>Sat,02Sep2014},{:foo3=>3,:date=>Sat,01Sep2014},{:foo4=>4,:date=>Sat,03Sep2014},{:foo5=>5,:date=>Sat,02Sep2014}]如果:date相同,我想合并哈希值。我对上面数组的期望是:[{:foo=>2,:foo3=>3,:date=>Sat,01Sep2014},{:foo2=>2,:foo5=>5:date=>Sat,02Sep2014},{:foo4=>4,:dat