Commit ce6fa36d by Damien Moulard

add ProductFamily entity & command to insert data

parent a3d264e3
<?php
declare(strict_types=1);
namespace App\Command;
use App\Entity\ProductFamily;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;
/**
* @SuppressWarnings(PHPMD.CyclomaticComplexity)
*/
class CreateProductFamiliesCommand extends Command
{
private const PRODUCTS_FAMILIES = [
"Féculents, céréales, farines",
"Légumineuses",
"Tubercules",
"Fruits et légumes",
"Champignons",
"Graines à germer",
"Viande et charcuterie",
"Œufs et produits laitiers",
"Poisson et produits de la mer",
"Miel et sucre",
"Pain du monde",
"Conserve",
"Fruits à coque",
"Huiles",
"Condiment et épices",
"Café, thé, cacao, chocolat, chicorée et tisanes"
];
protected static $defaultName = 'kohinos:tav:create-products-families';
protected $em;
protected $io;
public function __construct(
EntityManagerInterface $em
) {
$this->em = $em;
parent::__construct();
}
protected function configure()
{
$this
->setDescription('Insert product families into the database.')
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$this->io = new SymfonyStyle($input, $output);
$this->io->title('--- INSERTION DES FAMILLES DE PRODUITS ---');
$this->insertProductFamilies();
$this->io->success('--- FIN DE L\'INSERTION DES FAMILLES DE PRODUITS ---');
return 0;
}
private function insertProductFamilies()
{
foreach (self::PRODUCTS_FAMILIES as $productFamilyName) {
// Create all products families if don't exist
if (null == $this->em->getRepository(ProductFamily::class)->findOneBy(['name' => $productFamilyName])) {
$productFamily = new ProductFamily();
$productFamily->setName($productFamilyName);
$this->em->persist($productFamily);
}
$this->em->flush();
}
}
}
<?php
namespace App\Entity;
use App\Repository\ProductFamilyRepository;
use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\Doctrine\UuidGenerator;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Entity(repositoryClass=ProductFamilyRepository::class)
* @UniqueEntity(fields="name", message="Une famille de produits portant ce nom existe déjà.")
*/
class ProductFamily
{
/**
* @var \Ramsey\Uuid\UuidInterface
*
* @ORM\Id
* @ORM\Column(type="uuid", unique=true)
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class=UuidGenerator::class)
*/
private $id;
/**
* @ORM\Column(type="string", length=100, unique=true)
*/
private $name;
public function getId()
{
return $this->id;
}
/**
* Get name.
*
* @return string name
*/
public function getName(): ?string
{
return $this->name;
}
/**
* Set name.
*
* @return $this
*/
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
}
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240216140320 extends AbstractMigration
{
public function getDescription() : string
{
return '';
}
public function up(Schema $schema) : void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('CREATE TABLE product_family (id CHAR(36) NOT NULL COMMENT \'(DC2Type:uuid)\', name VARCHAR(100) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE `utf8_general_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE prestataire CHANGE iban iban LONGTEXT DEFAULT NULL COMMENT \'(DC2Type:personal_data)\'');
}
public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('DROP TABLE product_family');
$this->addSql('ALTER TABLE prestataire CHANGE iban iban LONGTEXT CHARACTER SET utf8mb3 DEFAULT NULL COLLATE `utf8mb3_general_ci` COMMENT \'(DC2Type:personal_data)\'');
}
}
<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240219140409 extends AbstractMigration
{
public function getDescription() : string
{
return '';
}
public function up(Schema $schema) : void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE prestataire CHANGE iban iban LONGTEXT DEFAULT NULL COMMENT \'(DC2Type:personal_data)\'');
$this->addSql('CREATE UNIQUE INDEX UNIQ_C79A60FF5E237E06 ON product_family (name)');
}
public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE prestataire CHANGE iban iban LONGTEXT CHARACTER SET utf8mb3 DEFAULT NULL COLLATE `utf8mb3_general_ci` COMMENT \'(DC2Type:personal_data)\'');
$this->addSql('DROP INDEX UNIQ_C79A60FF5E237E06 ON product_family');
}
}
<?php
namespace App\Repository;
use App\Entity\ProductFamily;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\ORM\OptimisticLockException;
use Doctrine\ORM\ORMException;
use Doctrine\Persistence\ManagerRegistry;
/**
* @extends ServiceEntityRepository<ProductFamily>
*
* @method ProductFamily|null find($id, $lockMode = null, $lockVersion = null)
* @method ProductFamily|null findOneBy(array $criteria, array $orderBy = null)
* @method ProductFamily[] findAll()
* @method ProductFamily[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class ProductFamilyRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, ProductFamily::class);
}
/**
* @throws ORMException
* @throws OptimisticLockException
*/
public function add(ProductFamily $entity, bool $flush = true): void
{
$this->_em->persist($entity);
if ($flush) {
$this->_em->flush();
}
}
/**
* @throws ORMException
* @throws OptimisticLockException
*/
public function remove(ProductFamily $entity, bool $flush = true): void
{
$this->_em->remove($entity);
if ($flush) {
$this->_em->flush();
}
}
// /**
// * @return ProductFamily[] Returns an array of ProductFamily objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('p')
->andWhere('p.exampleField = :val')
->setParameter('val', $value)
->orderBy('p.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?ProductFamily
{
return $this->createQueryBuilder('p')
->andWhere('p.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment