【发布时间】:2020-05-24 20:10:40
【问题描述】:
我为 DotA 2 解析比赛。通过第三方 API,我得到当前、未来和过去的比赛。但是我理解的问题是,updateOrCreate 函数不起作用。解析开始时,它会保存值,但不会更新当前值。这是我得到https://pastebin.com/KCcG6rJc的答案的一个例子,在这个答案中有球队现在在玩什么,有match_id的球队,如果球队互相竞争,这个值是一样的。使用这个解析结果的例子,我在数据库中收到了以下记录:
maches table,match_id 值到达那里 - 560004,没错。但是在指示这些命令的teams table 中,match_id 为空。
在我的解析器中,我有以下代码:
foreach ($live_matches as $live) {
if ($live->tournament_id != $tournament->id) {
continue;
}
$filtered_matches[] = $live;
foreach ($teams as &$team) {
if (false !== strpos($live->slug, $team->slug)) {
$team->match_id = $live->id;
$logo = $team->image_url;
var_dump($team);
exit;
}
}
}
var_dump($team); exit;说我:
object(stdClass)#1222 (8) {
["acronym"]=>
string(8) "ThunderP"
["id"]=>
int(2671)
["image_url"]=>
string(93) "https://cdn.pandascore.co/images/team/image/2671/87033C79D6D1A70A66941BC8649EA5988D74582C.png"
["location"]=>
string(2) "PE"
["modified_at"]=>
string(20) "2020-04-30T01:51:51Z"
["name"]=>
string(16) "Thunder Predator"
["slug"]=>
string(16) "thunder-predator"
["match_id"]=>
int(560004)
}
即我得到match_id,但在数据库中没有添加。
这是我的完整功能,更新或创建:
public function saveUpdate(int $tournament_id, array $tournament_teams, string $full_name, array $matches)
{
$tournament = Tournament::updateOrCreate([
'tournament_id' => $tournament_id,
'league_name' => $full_name
]);
foreach ($matches as $match) {
if ($match->status == 'not_started') {
$status = 0;
}
if ($match->status == 'running') {
$status = 1;
}
if ($match->status == 'finished') {
$status = 2;
}
$team = Tournament_teams::where('team_id', $match->winner_id)->first();
Tournament_matches::updateOrCreate([
'name' => $match->name,
'started_at' => Carbon::parse("$match->begin_at,")->setTimezone('Europe/Berlin'),
'ended_at' => $match->end_at,
'winner_id' => $team->id ?? null,
'status' => $status,
'match_id' => $match->id,
'live' => $match->live_url,
]);
}
foreach ($tournament_teams as $team) {
Tournament_teams::updateOrCreate([
'tournaments_id' => $tournament->id,
'team' => $team->name,
'slug' => $team->slug,
'logo' => $team->image_url,
'team_id' => $team->id,
], [
'match_id' => $team->match_id ?? null,
]);
}
}
例如,如果我从teams表中删除1列值并开始解析,则不会创建该列中的值。也就是说,它会创建数据,但不会更新。
现在有match_id 560334的球队正在比赛,而tournament_teams表没有这样的match_id,我按名称搜索比赛球队,发现数据库中有1个比赛球队3次一排。也就是说,一条记录有match_id = null,其他相同的match_id 命令有旧的。由于某种原因,当前的数据没有更新,并且在数据库中接收到空值,尽管 50% 的值是。这是带有突出显示的命令的屏幕截图,例如数据未更新:
我的错误在哪里?为什么var_dump 传给我match_id,但是这个值没有进入数据库?我的错在哪里?我使用 Laravel 5.1。如果您需要澄清任何细节 - 询问,谢谢您的帮助。
【问题讨论】: