Alex Rivera | Logout

Generate Symfony2 fixtures from DB?

Asked 2011-06-11T20:33:45.723
32

Is it possible to generate fixtures from an existing DB in Symfony2/Doctrine? How could I do that?

Example:

I have defined 15 entities and my symfony2 application is working. Now some people are able to browse to the application and by using it it had inserted about 5000 rows until now. Now I want the stuff inserted as fixtures, but I don’t want to do this by hand. How can I generate them from the DB?

Edit
Report

1 Answer

4

As I understand your question, you have two databases: the first is already in production and filled with 5000 rows, the second one is a new database you want to use for new test and development. Is that right ?

If it is, I suggest you to create in you test environment two entity manager: the first will be the 'default' one, which will be used in your project (your controllers, etc.). The second one will be used to connect to your production database. You will find here how to deal with multiple entity manager : http://symfony.com/doc/current/cookbook/doctrine/multiple_entity_managers.html

Then, you should create a Fixture class which will have access to your container. There is an "how to" here : http://symfony.com/doc/current/bundles/DoctrineFixturesBundle/index.html#using-the-container-in-the-fixtures.

Using the container, you will have access to both entity manager. And this is the 'magic': you will have to retrieve the object from your production database, and persist them in the second entity manager, which will insert them in your test database.

I point your attention to two points:

  • If there are relationship between object, you will have to take care to those dependencies: owner side, inversed side, ...
  • If you have 5000 rows, take care on the memory your script will use. Another solution may be use native sql to retrieve all the rows from your production database and insert them in your test database. Or a SQL script...

I do not have any code to suggest to you, but I hope this idea will help you.

answered 2012-08-30T09:06:14.990

Your Answer