fix: database seeder didn't update existing records

This commit is contained in:
2025-07-08 11:36:18 -05:00
parent 2c9138290a
commit 8c0ec98c20

View File

@@ -2,15 +2,20 @@
namespace App\Base\Framework\Command; namespace App\Base\Framework\Command;
use App\User\Framework\Entity\Preference;
use App\User\Framework\Entity\UserPreference; use App\User\Framework\Entity\UserPreference;
use App\User\Framework\Repository\PreferenceOptionRepository; use App\User\Framework\Repository\PreferenceOptionRepository;
use App\User\Framework\Repository\PreferencesRepository; use App\User\Framework\Repository\PreferencesRepository;
use App\User\Framework\Repository\UserRepository; use App\User\Framework\Repository\UserRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bridge\Doctrine\PropertyInfo\DoctrineExtractor;
use Symfony\Component\Console\Attribute\AsCommand; use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
#[AsCommand( #[AsCommand(
name: 'db:seed', name: 'db:seed',
@@ -50,17 +55,23 @@ class SeedDatabaseCommand extends Command
$preferences = $this->getPreferences(); $preferences = $this->getPreferences();
foreach ($preferences as $preference) { foreach ($preferences as $preference) {
if ($this->preferenceRepository->find($preference['id'])) { $isNewRecord = false;
continue; $preferenceRecord = $this->preferenceRepository->findOneBy(['id' => $preference['id']]);
if (null === $preferenceRecord) {
$isNewRecord = true;
$preferenceRecord = new Preference();
} }
$this->preferenceRepository->getEntityManager()->persist((new \App\User\Framework\Entity\Preference()) $preferenceRecord
->setId($preference['id']) ->setId($preference['id'])
->setName($preference['name']) ->setName($preference['name'])
->setDescription($preference['description']) ->setDescription($preference['description'])
->setEnabled($preference['enabled']) ->setEnabled($preference['enabled'])
->setType($preference['type']) ->setType($preference['type']);
);
if (true === $isNewRecord) {
$this->preferenceRepository->getEntityManager()->persist($preferenceRecord);
}
} }
$this->preferenceRepository->getEntityManager()->flush(); $this->preferenceRepository->getEntityManager()->flush();