DEV Community

Hanane Kacemi
Hanane Kacemi

Posted on

2

Symfony & Doctrine: Part 3

Previously, I talked about how to create easily an Entity (ex:JOB) using a simple command line make:Entity and answering a few question(fields,type…).

This article will be about saving data to, fetching data from, the database.

Saving Data to DB :

To do so, we need to use a service provided by Doctrine, called EntityManagerInterface. The command symfony console debug:autowiring doctrine shows all doctrine services available.

Type-hint an argument $em to EntityManagerInterface, create an Object (ex $job), set all its properties then call persist and flush methods.

Persist object to Db

Calling persist() method tells Doctrine that we want to save the object, no query is executed yet. We call this method as many time as objects that we have, once we call flush() method, Doctrine execute the queries needed to insert to database.

We can check if data has been inserted with : symfony console doctrine:query:sql 'select * from job'

Data persisted to db

Fetching Data from DB :

Getting data(objects) from db is even easier, one way to do that is by using JobRepository $jobRepo as an argument in our method.

Fetch Data from DB

The repository class offers many helper methods to get our data from db like : findBy, findOneBy... We can also add custom methods to have complex queries, more about that in my next article.

Finally, I like to mention that Profiler provides a lot of tools to use when developing an app, for doctrine, it shows all queries executed :

Queries in Profiler

Thanks for reading and Happy new year :)

Do your career a big favor. Join DEV. (The website you're on right now)

It takes one minute, it's free, and is worth it for your career.

Get started

Community matters

Top comments (1)

Collapse
 
hosam-kyali profile image
Hosam Kyali

I have simple question what if I want to retrieve URL for many paths stored in database how just the render will handle it using twig?
just at your explanation you mention that inside findby
for example I have avatar path stored in database like this ('image/avatar/1.jpb') then I have another one with two images but ('image/avatar/1.jpb'),(image/avatar/2.jpb') I just curious how I can add both in render output?

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay