【问题标题】:Laravel updateOrcreate missing valueLaravel 更新或创建缺失值
【发布时间】:2020-05-24 20:10:40
【问题描述】:

我为 DotA 2 解析比赛。通过第三方 API,我得到当前、未来和过去的比赛。但是我理解的问题是,updateOrCreate 函数不起作用。解析开始时,它会保存值,但不会更新当前值。这是我得到https://pastebin.com/KCcG6rJc的答案的一个例子,在这个答案中有球队现在在玩什么,有match_id的球队,如果球队互相竞争,这个值是一样的。使用这个解析结果的例子,我在数据库中收到了以下记录:

maches tablematch_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。如果您需要澄清任何细节 - 询问,谢谢您的帮助。

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    您错误地使用了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 版本。

    【讨论】:

    • 我将在 1 周内更新到较新的版本,我以您的示例为例,但是在Tournament_teams 丢失了很多数据我的意思是,我只收到了match_id,其中match_id null我没有收到球队,但现在有 2 场现场比赛,而且我没有在我的桌子上收到 ?
    • 起来,对不起。我的错。 :( 根据您提供的信息,您的代码有点抽象。我认为问题可能出在updateOrCreate 方法中。最重要的一点是,您知道它是如何工作的吗?如果您撤消更改,您的表格是又填满了吗?
    • 别担心,一切都很好:),是的,如果我撤消更改,表格会再次填满。让我试着描述问题。现在有 AllianceNiP 2 支球队的比赛,他们有 match_id = 561520,在`tournament_matches` 中,这场比赛与match_id 相同,但在tournament_teams 中,match_id 为空.如果我在数据库中搜索 Alliance,我会得到 4 个结果,其中 match_id 为空,4 个结果 NiP 为空 match_id,这是主要问题
    • 什么是AllianceNiP?无论如何,对不起,但我不能 100% 理解你的问题。我需要一个 UML 表示或其他东西。我的建议是首先检查tournament_teams 是否已填写并且正确,最后,按照您的逻辑填写tournament_matches 等等。尝试使用虚拟数据并进行调查。您可以尝试使用原始 SQL 查询 (DB::select()),一旦它起作用,我建议您切换到使用 Eloquent 模型。干杯! :)
    猜你喜欢
    • 1970-01-01
    • 2021-11-12
    • 2014-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多