我会解析DotA 2的匹配项。通过第三方API,我可以获得当前,将来和过去的匹配项。但是我所理解的问题是updateOrCreate
函数无法正常工作。解析开始时,它将保存值,但不会更新当前值。这是我得到https://pastebin.com/KCcG6rJc的答案的示例,在这个答案中,有现在正在玩的球队,有match_id
个球队,如果两个球队互相竞争,则此值为相同。使用此解析结果的示例,我在数据库中收到以下记录:
maches table
和match_id
值就到了-560004
,没错。但是在teams table
(指示这些命令)中,match_id为null。
在我的解析器中,我有以下代码:
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。如果您需要澄清任何细节-请询问,谢谢您的帮助。
答案 0 :(得分:0)
您错误地使用了updateOrCreate()
方法。
您可能还会遇到想要更新现有模型或如果不存在则创建新模型的情况。
// If there's a flight from Oakland to San Diego, set the price to $99.
// If no matching model exists, create one.
$flight = App\Flight::updateOrCreate(
['departure' => 'Oakland', 'destination' => 'San Diego'],
['price' => 99, 'discounted' => 1]
);
如果我很好地理解了您的问题,那么您正在采取相反的方式:
Tournament_teams::updateOrCreate([
'match_id' => $team->match_id ?? null,
], [
'tournaments_id' => $tournament->id,
'team' => $team->name,
'slug' => $team->slug,
'logo' => $team->image_url,
'team_id' => $team->id,
]);
此外,为此,您无需使用updateOrCreate()
,例如,firstOrCreate()
:
$tournament = Tournament::firstOrCreate([
'tournament_id' => $tournament_id,
'league_name' => $full_name
]);
PS:如果可能,我建议您使用较新的Laravel版本。