}
foreach ($data as $row) {
- $model = new StoreCityList();
- $model->name = $row['name'] ?? $row['A'];
- $model->parent_id = $row['parent_id'] ?? $row['B'];
- $model->type = $row['type'] ?? $row['C'];
-
- if (!$model->save()) {
- Yii::error("Ошибка при сохранении: " . json_encode($model->errors));
+ $name = $row['name'] ?? $row['A'];
+ $parent_id = $row['parent_id'] ?? $row['B'];
+ $type = $row['type'] ?? $row['C'];
+
+ $existingRecord = StoreCityList::find()
+ ->where(['name' => $name, 'parent_id' => $parent_id, 'type' => $type])
+ ->exists();
+
+ if (!$existingRecord) {
+ $model = new StoreCityList();
+ $model->name = $name;
+ $model->parent_id = $parent_id;
+ $model->type = $type;
+
+ if (!$model->save()) {
+ Yii::error("Ошибка при сохранении: " . json_encode($model->errors));
+ }
+ } else {
+ Yii::info("Пропущена запись: $name (parent_id: $parent_id, type: $type), так как она уже существует.");
}
}
}