DEV Community

Hanane Kacemi
Hanane Kacemi

Posted on

4 1

Symfony & Doctrine: Part 4

While developing a new app, we usually don’t have any data on DB, it’s common to add fake data to do our tests, either manually or using libraries. With Symfony we have a bundle called “Doctrine Fixture”, I tried it and below are my notes about it.

To install it, use: composer require orm-fixtures --dev
New folder “DataFixtures” is created under “src”, it will contain PHP Classes where we create objects to persist into DB.

I like to keep things organized so I will create many fixtures classes instead of adding all dummy data to AppFixtures generated by default.

In order to reuse objects across fixtures Classes we use object References(line 32 to 34) and we can get easily the reference in the 2nd Fixture Class (line 15)

Category Fixtures
Job Fixtures

When using multiple fixtures, we need to specify the order in which fixtures will be loaded otherwise Doctrine will use the alphabetical order, which may result in errors if objects referenced are not yet created. To do so, we need to implement DependentFixtureInterface and then add getDependencies() to specify fixtures to load before.
Fixtures dependencies

To add data to db, we run : symfony console doctrine:fixtures:load

I have also tried an other bundle that helps to generate dummy data instead of setting values manually (if you lack inspiration like me :D ) to add it : composer require fzaninotto/faker --dev

Once installed, we can use methods availables in this library to get a name, address, country, slug, numbers... It's awesome.

Below my JobFixtures class after using this library
JobFixtures using Faker

more infos on the documentation : https://github.com/fzaninotto/Faker

Thanks for reading :)

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read full post →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay