0% ont trouvé ce document utile (0 vote)
15 vues3 pages

Atelier de Reverse Engineering Symfony 5.4

Ce document décrit un workshop Symfony sur l'ingénierie inverse. Il présente les étapes pour générer des entités à partir d'une base de données existante avec deux tables Etudiant et Classe et configurer l'application.

Transféré par

moslem.haddadi
Copyright
© All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats PDF, TXT ou lisez en ligne sur Scribd
0% ont trouvé ce document utile (0 vote)
15 vues3 pages

Atelier de Reverse Engineering Symfony 5.4

Ce document décrit un workshop Symfony sur l'ingénierie inverse. Il présente les étapes pour générer des entités à partir d'une base de données existante avec deux tables Etudiant et Classe et configurer l'application.

Transféré par

moslem.haddadi
Copyright
© All Rights Reserved
Nous prenons très au sérieux les droits relatifs au contenu. Si vous pensez qu’il s’agit de votre contenu, signalez une atteinte au droit d’auteur ici.
Formats disponibles
Téléchargez aux formats PDF, TXT ou lisez en ligne sur Scribd

Année Universitaire : 2023-2024

Workshops Framework Symfony5.4

Workshop n°2 :
Reverse engineering
Objectif :

● Génération des Entités à partir d'une base de données existante.

Etude de cas :

- Créer un projet Symfony5.4 nommé « ReverseEngineering »


- Créer une base de données « esprit » avec les deux tables Etudiant et Classe comme le
montre le diagramme de classes suivant :

Etudiant Classe

id id
* 1
username nom

moyenne

- Configurer vos paramètres de connexion à la base de données dans le fichier « .env » en


modifiant le nom de la base de données « symfony » par celui de la base de données (dans
notre cas « esprit »).
NB : Assurez-vous toujours d’utiliser le même serveur de donnée
(mariaDB/MySQL)

- Générer les entités en tapant la commande :

php bin/console doctrine:mapping:import "App\Entity" annotation --path=src/Entity

- Générer les méthodes getters et setters des entités importées en tapant la commande :
php bin/console make:entity --regenerate App

- Changer dans l’entité les annotations par les attributs :

Annotation Attribute
- Ajouter le "use repository" au début du fichier et ajouter la ligne suivante avant la définition
de la classe :

- Pour générer les repository vous devez taper la commande :

php bin/console make:entity --regenerate

Résultat : vous devez avoir un répertoire Entity qui contient deux classes PHP
Etudiant et Classe.

Common questions

Alimenté par l’IA

The specific PHP command used for importing database mapping into Symfony entities is 'php bin/console doctrine:mapping:import "App\Entity" annotation --path=src/Entity'. This command is executed to import the existing database structure as entity classes within the Symfony framework, essentially allowing the application to recognize and interact with the database schema defined in tables 'Etudiant' and 'Classe'. The purpose of this command is to facilitate initial entity generation that serves as the blueprint for further development within the Symfony application .

The primary objective of the Symfony 5.4 workshop is to generate entities from an existing database, specifically focusing on reverse engineering. This involves creating a project named 'ReverseEngineering', using a database named 'esprit', and configuring the necessary parameters for connecting to the database. The process includes generating entities and their methods such as getters and setters, and adding necessary annotations and attributes .

Checking and configuring Symfony project parameters is important to ensure that the connection settings align with the actual database environment. Without proper configuration, the entity generation commands may not execute correctly, potentially causing mismatches in database schema recognition. Ensuring parameters like database name and server type (e.g., MariaDB/MySQL) are configured correctly minimizes the risk of errors and ensures smooth integration between the Symfony application and its database .

The command used to regenerate entities and repository classes is 'php bin/console make:entity --regenerate'. Executing this command leads to the creation of a directory 'Entity' that contains PHP classes for both 'Etudiant' and 'Classe'. This also involves the generation of repository classes which are essential for querying the database efficiently .

Annotations and attributes play a crucial role in defining metadata for entity transition and recognition. During the entity generation process, annotations are initially used, but they are later replaced by attributes as per the instructions given in the document. This involves editing the entity files to ensure PHP's attribute syntax is used instead of annotations, aligning the code with modern PHP standards .

The 'use repository' instruction is significant because it ensures that entity classes in Symfony are linked to their corresponding repository classes, which manage data retrieval operations. Including this instruction at the beginning of the entity files allows developers to leverage Doctrine's repository pattern, facilitating efficient data handling and enabling sophisticated querying capabilities within the Symfony application .

The transformation process from annotations to attributes in Symfony 5.4 involves editing each entity class file to replace the annotations, which are used to describe properties and relationships, with PHP attributes. This shift is necessary to adhere to PHP's attribute syntax, enhancing code readability and performance. Attributes provide a modern alternative to annotations, aligning PHP syntax more closely with current language features, and are directly parsed by the PHP engine, offering better execution efficiency .

The workshop guide suggests generating entity classes from an existing database by first typing the command 'php bin/console doctrine:mapping:import "App\Entity" annotation --path=src/Entity' to initiate the import process. This procedure imports database tables as entities into the Symfony project directory. After the import, methods such as getters and setters are generated for the entities using additional command-line instructions .

Using a consistent data server like MariaDB/MySQL is crucial because it ensures uniformity and compatibility of SQL commands during database operations. Given that the workshop involves generating entities based on database structures, consistency in the data server ensures that the database schema is interpreted correctly and reliably across development environments, reducing the risk of errors during entity regeneration .

To configure a database connection for the Symfony project, you need to modify the connection parameters in the '.env' file. This involves changing the database name from the default 'symfony' to 'esprit', ensuring that the server is MariaDB/MySQL, and then running the command to generate entities within the properly configured environment .

Vous aimerez peut-être aussi