Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente | ||
|
prog:symfony:entities:entity [27/10/2019 14:36] thierry [make:migration] |
prog:symfony:entities:entity [13/09/2022 16:17] (Version actuelle) thierry [Création] |
||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| ====== Symfony et les Entities (Entity)====== | ====== Symfony et les Entities (Entity)====== | ||
| - | Avant de jouer avec les Entities il faut que la [[prog:symfony:databases|base de données soit paramétrée et créée]]. | + | ===== Prérequis ===== |
| + | |||
| + | Avant de jouer avec les Entities il faut que la [[prog:symfony:doctrine:databases|base de données soit paramétrée et créée]]. | ||
| + | |||
| + | -> Source : [[https://symfony.com/doc/current/doctrine.html#configuring-the-database]] | ||
| + | |||
| + | ===== Entity ===== | ||
| + | |||
| + | Une Entity est un **Objet PHP** qui sera stocké en base de donnée via l'ORM de Symfony | ||
| + | |||
| + | Pour créer une Entity on utilisera la commande ''symfony console make:entity %EntityName%'' (voir ci-dessous). | ||
| + | |||
| + | -> Source : [[https://symfony.com/doc/current/doctrine.html#creating-an-entity-class]] | ||
| ===== Création d'une Entity ===== | ===== Création d'une Entity ===== | ||
| Ligne 12: | Ligne 24: | ||
| * **Online_Date** : Date | * **Online_Date** : Date | ||
| ==== make:entity ==== | ==== make:entity ==== | ||
| - | Avec la console ''php bin\console make:entity'' | + | Avec la console ''php bin\console make:entity'' ou ''symfony console make:entity'' |
| <code> | <code> | ||
| D:\webprojects\test-api-project>php bin\console make:entity | D:\webprojects\test-api-project>php bin\console make:entity | ||
| Ligne 298: | Ligne 310: | ||
| </code> | </code> | ||
| + | === Fichier Repository généré === | ||
| + | <code php src/Repository/ProjectsRepository.php> | ||
| + | <?php | ||
| + | |||
| + | namespace App\Repository; | ||
| + | |||
| + | use App\Entity\Projects; | ||
| + | use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; | ||
| + | use Doctrine\Common\Persistence\ManagerRegistry; | ||
| + | |||
| + | /** | ||
| + | * @method Projects|null find($id, $lockMode = null, $lockVersion = null) | ||
| + | * @method Projects|null findOneBy(array $criteria, array $orderBy = null) | ||
| + | * @method Projects[] findAll() | ||
| + | * @method Projects[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null) | ||
| + | */ | ||
| + | class ProjectsRepository extends ServiceEntityRepository | ||
| + | { | ||
| + | public function __construct(ManagerRegistry $registry) | ||
| + | { | ||
| + | parent::__construct($registry, Projects::class); | ||
| + | } | ||
| + | |||
| + | // /** | ||
| + | // * @return Projects[] Returns an array of Projects 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): ?Projects | ||
| + | { | ||
| + | return $this->createQueryBuilder('p') | ||
| + | ->andWhere('p.exampleField = :val') | ||
| + | ->setParameter('val', $value) | ||
| + | ->getQuery() | ||
| + | ->getOneOrNullResult() | ||
| + | ; | ||
| + | } | ||
| + | */ | ||
| + | } | ||
| + | |||
| + | </code> | ||
| + | |||
| Ligne 379: | Ligne 446: | ||
| === Résultats de la migration === | === Résultats de la migration === | ||
| {{:prog:symfony:table_projetcs.png|}} | {{:prog:symfony:table_projetcs.png|}} | ||
| + | ====== Resources ====== | ||
| + | * [[https://symfony.com/doc/current/doctrine.html]] | ||
| + | |||