Commande pour peupler la db

cleo
Cleo EIRAS 6 days ago
parent edc8730198
commit 3406dc14f4

@ -0,0 +1,62 @@
<?php
namespace App\Command;
use Doctrine\DBAL\Connection;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class PopulateDBCommand extends Command
{
protected static $defaultName = 'app:populateDB';
private Connection $connection;
public function __construct(Connection $connection)
{
parent::__construct();
$this->connection = $connection;
}
protected function configure()
{
$this
->setDescription('Populate the database.');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
try {
// On supprime la table si elle existe déjà
$this->connection->executeStatement('DROP TABLE IF EXISTS rarity');
// On crée la table
$this->connection->executeStatement('
CREATE TABLE rarity (
id INT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
drop_rate FLOAT NOT NULL
)
');
// On peuple la table
$this->connection->executeStatement("INSERT INTO rarity (id, name, drop_rate) VALUES
(1, 'Common', 0.6),
(2, 'Rare', 0.2),
(3, 'Epic', 0.1),
(4, 'Mythical', 0.085),
(5, 'Legendary', 0.015)
");
$output->writeln('Base de données peuplée.');
} catch (\Exception $e) {
$output->writeln('<error>Erreur : ' . $e->getMessage() . '</error>');
return Command::FAILURE;
}
return Command::SUCCESS;
}
}
Loading…
Cancel
Save