Laravel更新或创建缺失值

问题描述 投票:0回答:1

我会解析DotA 2的匹配项。通过第三方API,我可以获得当前,将来和过去的匹配项。但是我怎么理解的问题,它的updateOrCreate功能不起作用。解析开始时,它将保存值,但不会更新当前值。这是我得到https://pastebin.com/KCcG6rJc的答案的示例,此答案中有现在正在玩的球队,有match_id个球队,如果两个球队互相竞争,则该值相同。使用此解析结果的示例,我在数据库中收到以下记录:

maches tablematch_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%会更新。这是带有突出显示的命令的屏幕截图,例如,这些命令的数据未更新:Screenshot

我的错误在哪里?为什么var_dump传递给我match_id,但是这个值没有进入数据库?我的错误在哪里?我使用Laravel 5.1。如果您需要澄清任何细节,请询问,谢谢您的帮助。

php laravel
1个回答
0
投票

您在错误地使用updateOrCreate()方法。

作为documentation says


您可能还会遇到想要更新现有模型或创建新模型(如果不存在的情况。)>

// 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版本。

© www.soinside.com 2019 - 2024. All rights reserved.