Laravel create()方法是否可以插入双倍时间相同的值?

时间:2018-02-05 18:22:34

标签: php laravel

我的一个Laravel脚本有一个非常奇怪的情况。我有一个爬虫脚本,它从其他网站获取值并将它们保存到我的数据库中。为了便于检查我已经具有该值,我将其他网站的主键保存到我的数据库中。在每次插入之前(Laravel create()方法)我检查我的表中是否存在该键,如果它不存在,那么我可以插入该值。这很简单。不幸的是有时候(我找不到任何重复的情况)我对另一个网站的同一主键有双重值。

我安装了laravel-debugbar,当我检查所有数据库查询时,没有双插入查询,这很奇怪。 我已经输出以检查我的某些循环是否导致了这个问题,但是没有。

以下是我的一些代码,如果有人有其他线索,为什么会发生这种情况:

$check = FootballGame::where('foreign_id', '=', $key)->get();
// echo $key." ".count($check)."<br>\n";

if(count($check) == 0) {
    if(strip_tags($objResultGame[5][0]) != 'CAN.' && strip_tags($objResultGame[5][0]) != 'POSTP.') {
        $stats = $this->get_game_stats($this->domain.$objResultGame[1][0], $sport_id);
        if(!empty($stats)) {
            FootballGame::create([
                'foreign_id' => $key,
                'season_id' => $season->id, 
                'stage_id' => (($stage_id > 0) ? $stage_id : null), 
                'round_type' => $gr, 
                'round' => $r, 
                'group_id' => ($group_id == 0) ? null : $group_id, 
                'datetime' => $stats['datetime'], 
                'team1_id' => $stats['team1'], 
                'team2_id' => $stats['team2'],
                'home' => $stats['home'], 
                'away' => $stats['away'], 
                'home_ht' => $stats['home_ht'], 
                'away_ht' => $stats['away_ht'], 
                'overtime' => $stats['overtime'], 
                'winner_team_id' => $stats['winner_team_id'], 
                'comment' => $stats['comment'], 
                'status' => 1
            ]);
            echo "Add match: ".$group_id." ".date('Y-m-d H:i:s')."<br>\n";
        }
    }
}

数据库架构:

CREATE TABLE `games_football` (
  `id` int(10) UNSIGNED NOT NULL,
  `foreign_id` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
  `season_id` int(10) UNSIGNED NOT NULL,
  `stage_id` int(10) UNSIGNED DEFAULT NULL,
  `group_id` smallint(6) DEFAULT NULL,
  `round_type` smallint(6) NOT NULL,
  `round` smallint(6) DEFAULT NULL,
  `datetime` datetime NOT NULL,
  `team1_id` int(10) UNSIGNED NOT NULL,
  `team2_id` int(10) UNSIGNED NOT NULL,
  `home` smallint(6) NOT NULL,
  `away` smallint(6) NOT NULL,
  `home_ht` smallint(6) NOT NULL,
  `away_ht` smallint(6) NOT NULL,
  `overtime` varchar(20) COLLATE utf8_unicode_ci DEFAULT NULL,
  `winner_team_id` int(10) UNSIGNED DEFAULT NULL,
  `comment` text COLLATE utf8_unicode_ci,
  `status` smallint(6) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `games_football`
--
ALTER TABLE `games_football`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `games_football_foreign_id_unique` (`foreign_id`),
  ADD KEY `games_football_season_id_index` (`season_id`),
  ADD KEY `games_football_stage_id_index` (`stage_id`),
  ADD KEY `games_football_group_id_index` (`group_id`),
  ADD KEY `games_football_team1_id_index` (`team1_id`),
  ADD KEY `games_football_team2_id_index` (`team2_id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `games_football`
--
ALTER TABLE `games_football`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT;COMMIT;

1 个答案:

答案 0 :(得分:0)

也许您需要更多限制检查

$game = FootballGame::where('foreign_id', '=', $key)->first();

if(!$game->exists) {

     //other code here

}