Skip to content
Advertisement

How to fetch entities in db while using doctrine fixtures?

I have been the ContainerAwareInterface on my LoadAdvert class so I can call and use the EntityManager to retrieve users in the database.

However, when executing php bin/console doctrine:fixtures:load, the entity manager retrieves only an empty array, as if there was no user in the db.

JavaScript

Advertisement

Answer

Here it is how I do it. The main thing is to set reference for user objects that you create and then call it in other fixture file and assign them to related objects. Also its important the order of execution of fixture data. You cant load related object before you load users.

JavaScript

Then in other fixture file you dont call user as you did from database, you do it like this:

JavaScript

and then you add this $author object to related object.

Keep in mind to implement OrderedFixtureInterface in this article fixture and set getOrder() to be higher number than the one for users.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement