Datos iniciales
Configuraci贸n inicial
Entidades
Conceptos
Relaciones
ManyToOne, OneToMany, ManyToMany y OneToOne
Carga de datos falsos
F谩brica de datos
Proceso de creaci贸n de datos falsos
Proyecto
Frontend
Plantilla web
Listado de registros
Filtro por etiqueta
Ver el detalle de un registro
Listado de comentarios
Consulta de datos
createQuery
createQueryBuilder
Optimizaci贸n del listado total
Optimizaci贸n filtro por etiquetas y comentarios
Siempre se puede mejorar
Acceso a datos
Despedida
Repaso de lo construido
You don't have access to this class
Keep learning! Join and start boosting your career
Loading dummy data into a database can be an essential step when developing an application. This allows us to test the application in an environment quite similar to the real world. In this process, we use the Symfony framework and we rely on Visual Studio Code to execute several commands that make possible the generation of this dummy data.
First, we need to make sure we have all the necessary components to create and handle dummy data. We use the phpbing console make fixture
command, which when run will tell you that you need the doctrine/doctrine-fixtures-bundle
package to proceed. So, we install it with the command:
composer require doctrine/doctrine-fixtures-bundle --dev
After installation, you will see that a new folder and file are generated in the project structure, where you can define the structure of your fake data.
You can start by uncommenting the example template and adapting it to the characteristics of the data you wish to generate. Here is a basic example of how you could proceed to create a product with metadata:
// Create product$product = new Product();$product->setName('test product');$product->setSummary('test summary');
// Create metadata associated with the product$metadata = new Metadata();$metadata->setCode(rand(100, 200));
$metadata->setContent('official product content');
// Persist metadata and associate it with the product$manager->persist($metadata);$product->addMetadata($metadata);
// Prepare to save$manager->persist($product);$manager->flush();
Remember to import the necessary classes. Here we are working with Product
and Metadata
, as the example indicates.
After defining your data, you can run the command to load this fake information into the database. The command will be something similar to:
bin/console doctrine:fixtures:load
This command will ask for confirmation before proceeding to insert the data into the database. Once you confirm, the data will be loaded successfully.
In addition to creating products and metadata, you can expand your dummy data to include tags and comments, which will enrich your testing and usage scenarios. Here's an example of how you could add tags and comments:
// Create tags$tag1 = new Tag();$tag1->setName('tag 1');$manager->persist($tag1);
$tag2 = new Tag();$tag2->setName('tag 2');$manager->persist($tag2);
// Associate tags to the product$product->addTag($tag1);$product->addTag($tag2);
// Create comments$comment1 = new Comment();$comment1->setContent('comment number 1');
$manager->persist($comment1);
$comment2 = new Comment();$comment2->setContent('comment number 2');$manager->persist($comment2);
// Associate comments to the product$product->addComment($comment1);$product->addComment($comment2);
// Prepare and save everything$manager->persist($product);$manager->flush();
Learning how to handle this data allows to better understand the data structure of the Symfony framework and paves the way to design efficient controllers. In the end, this dummy data brings your development environment closer to production reality, facilitating more accurate testing and more rigorous quality control.
Once you have mastered this initial testing, consider using random data libraries to simulate more realistic inputs, such as real names and more complex comments. This will significantly improve the value of automated testing and ensure that your test results are as accurate as possible. Keep practicing and developing your Symfony skills!
Contributions 0
Questions 0
Want to see more contributions, questions and answers from the community?