我有需要在 db 中导入的 json 数据。在持久化实体之前,我需要检查数据库中的两个字段值。如果它们存在,则跳过它们而不抛出错误,如果不创建仅缺少的那个。
$file = file_get_contents('file.json');
$jsonData = json_decode($file, true);
$check = $this->getMyRepository()->findOneBy([
'first_name' => $firstName,
'last_name' => $lastName
]);
foreach ($jsonData as $data) {
if ($check) {
continue;
} else {
$new = new MyEntity();
$new->setFirstName($data->getFirstName());
$new->setLastName($data->getLastName());
$this->em->persist($new);
}
}
}
$this->em->flush();
}
导入正在运行,但是当我触发 api 时,它总是导入所有值,而不应该像我提到的那样。
慕妹3242003