Browse Source

creating entity product

master
art.dambrine 4 years ago
parent
commit
4eb99a862a
  1. 31
      migrations/Version20210323164758.php
  2. 18
      readme.md
  3. 41
      src/Entity/Product.php
  4. 50
      src/Repository/ProductRepository.php

31
migrations/Version20210323164758.php

@ -0,0 +1,31 @@
<?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 Version20210323164758 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 (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(255) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
}
public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('DROP TABLE product');
}
}

18
readme.md

@ -10,3 +10,21 @@ Environnement de developpement mis en place avec [symfony-docker-init **@gitea**
- chargement d'assets au travers de twig
- passage de variables du controller vers la vue twig
### Cours 2 - Introduction à l'ORM Doctrine (23/03/2021)
Objectif de l'ORM, lier la structure de la base de données à ses `Entity` php.
Pas besoin de se connecter à la console de la base de données en CLI (dans l'idéal)
Dans l'idéal nous souhaitons tout faire via le code php et la CLI PHP de Symfony.
**Relations**
- On fera en sorte que l'objet qui a "un seul" va porter la liaison
*NOTE: Pattern singloton (cf. pour le requêtage à BDD)*

41
src/Entity/Product.php

@ -0,0 +1,41 @@
<?php
namespace App\Entity;
use App\Repository\ProductRepository;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass=ProductRepository::class)
*/
class Product
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
*/
private $name;
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
}

50
src/Repository/ProductRepository.php

@ -0,0 +1,50 @@
<?php
namespace App\Repository;
use App\Entity\Product;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
/**
* @method Product|null find($id, $lockMode = null, $lockVersion = null)
* @method Product|null findOneBy(array $criteria, array $orderBy = null)
* @method Product[] findAll()
* @method Product[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class ProductRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Product::class);
}
// /**
// * @return Product[] Returns an array of Product 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): ?Product
{
return $this->createQueryBuilder('p')
->andWhere('p.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}
Loading…
Cancel
Save