【问题标题】:How to update column in table if api results have been modified如果修改了 api 结果,如何更新表中的列
【发布时间】:2018-10-29 15:06:53
【问题描述】:

我的应用程序:我的应用程序允许用户预测下一个比赛日所有即将举行的足球比赛的比分。我从 API 获取这些数据并将即将推出的游戏存储在我的数据库中。这些即将推出的游戏有statusScheduled。现在我想每隔几分钟运行一次 cronjob,检查这些匹配的状态是否已更改为 in_playfinished,如果是这种情况,我想在我的数据库中更新我的 status field 以获得正确的匹配从 api 到状态字段。

如何检查状态是否已更改并在我的数据库中修改正确的匹配项?我存储了一个 match_id 可以用于此目的吗?

我的代码: updateStatus工作

public function handle()
    {
        $this->updateStatus();
    }

    public function updateStatus() {

        $this->getMatches();

        // check if status has been changed from schedulded to 'in_play' or 'finished'
        // update the match status of the right matches in my database
    }

    public function getMatches() {

        $client = new Client();
        $uri = 'http://api.football-data.org/v2/competitions/PL/matches/?matchday=12&season=2018&matches';
        $header = ['headers' => ['X-Auth-Token' => 'My-token']];
        $res = $client->get($uri, $header);
        return json_decode($res->getBody()->getContents(), true);

    }

getMatchesjob(这个job获取api数据并存入数据库)

 public function handle()
    {
        $this->saveMatches();
    }

    public function saveMatches()
    {
        $matches = $this->getMatches();

        collect($matches['matches'])
            ->each(function ($match, $key) {
                $date = new DateTime($match['utcDate']);
                Match::create([
                    'match_id' => $match['id'],
                    'homeTeam' => $match['homeTeam']['name'],
                    'awayTeam' => $match['awayTeam']['name'],
                    'status'   => $match['status'],
                    'date'     => $date->format('Y-m-d'),
                    'time'     => $date->format('H:i'),
                    'matchday' => $match['matchday'],
                    'homeScore'=> $match['score']['fullTime']['homeTeam'],
                    'awayScore'=> $match['score']['fullTime']['awayTeam']
                ]);
            });


    }
    public function getMatches()
    {
        $client = new Client();
        $uri = 'http://api.football-data.org/v2/competitions/PL/matches/?matchday=12&season=2018&matches';
        $header = ['headers' => ['X-Auth-Token' => 'My-token']];
        $res = $client->get($uri, $header);
        return json_decode($res->getBody()->getContents(), true);
    }

【问题讨论】:

    标签: php laravel laravel-jobs


    【解决方案1】:

    你可能想要做的是在你的 Match 对象上使用 Laravel 的 updateOrCreate() 方法。唯一标识信息似乎是匹配 ID。如果这永远不会改变,那么当您遍历每个语句时,您可以这样做:

    Match::updateOrCreate([
            'id' => $match['id'],
        ],[
            'homeTeam' => $match['homeTeam']['name'],
            'awayTeam' => $match['awayTeam']['name'],
            'status'   => $match['status'],
            'date'     => $date->format('Y-m-d'),
            'time'     => $date->format('H:i'),
            'matchday' => $match['matchday'],
            'homeScore'=> $match['score']['fullTime']['homeTeam'],
            'awayScore'=> $match['score']['fullTime']['awayTeam']
        ]);
    

    它的作用是查找具有相同 ID 的现有匹配项。如果它已经存在,它会简单地使用 API 提供的所有信息来更新它,包括比赛的状态和分数。如果它还不存在,它将创建它并将其存储在数据库中,作为与所有提供的信息的新匹配。希望这会有所帮助!

    【讨论】:

    • updateOrCreate 方法正是我在我的情况下所需要的,非常好的解决方案!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-12
    • 1970-01-01
    • 2020-10-01
    • 2023-02-13
    • 1970-01-01
    • 2018-09-04
    相关资源
    最近更新 更多