Generate Symfony2 fixtures from DB? Generate Symfony2 fixtures from DB? symfony symfony

Generate Symfony2 fixtures from DB?


There's no direct manner within Doctrine or Symfony2, but writing a code generator for it (either within or outside of sf2) would be trivial. Just pull each property and generate a line of code to set each property, then put it in your fixture loading method. Example:

<?php$i = 0;$entities = $em->getRepository('MyApp:Entity')->findAll();foreach($entities as $entity){   $code .= "$entity_{$i} = new MyApp\Entity();\n";   $code .= "$entity_{$i}->setMyProperty('" . addslashes($entity->getMyProperty()); . "'); \n");   $code .= "$manager->persist($entity_{$i}); \n $manager->flush();";   ++$i;}// store code somewhere with file_put_contents


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.


I assume that you want to use fixtures (and not just dump the production or staging database in the development database) because a) your schema changes and the dumps would not work if you update your code or b) you don't want to dump the hole database but only want to extend some custom fixtures. An example I can think of is: you have 206 countries in your staging database and users add cities to those countries; to keep the fixtures small you only have 5 countries in your development database, however you want to add the cities that the user added to those 5 countries in the staging database to the development database

The only solution I can think of is to use the mentioned DoctrineFixturesBundle and multiple entity managers.

First of all you should configure two database connections and two entity managers in your config.yml

doctrine:    dbal:        default_connection: default        connections:            default:                driver:   %database_driver%                host:     %database_host%                port:     %database_port%                dbname:   %database_name%                user:     %database_user%                password: %database_password%                charset:  UTF8            staging:                ...    orm:        auto_generate_proxy_classes: %kernel.debug%        default_entity_manager:   default        entity_managers:            default:                connection:       default                mappings:                    AcmeDemoBundle: ~            staging:                connection:       staging                mappings:                    AcmeDemoBundle: ~

As you can see both entity managers map the AcmeDemoBundle (in this bundle I will put the code to load the fixtures). If the second database is not on your development machine, you could just dump the SQL from the other machine to the development machine. That should be possible since we are talking about 500 rows and not about millions of rows.

What you can do next is to implement a fixture loader that uses the service container to retrieve the second entity manager and use Doctrine to query the data from the second database and save it to your development database (the default entity manager):

<?phpnamespace Acme\DemoBundle\DataFixtures\ORM;use Doctrine\Common\DataFixtures\FixtureInterface;use Doctrine\Common\Persistence\ObjectManager;use Symfony\Component\DependencyInjection\ContainerAwareInterface;use Symfony\Component\DependencyInjection\ContainerInterface;use Acme\DemoBundle\Entity\City;use Acme\DemoBundle\Entity\Country;class LoadData implements FixtureInterface, ContainerAwareInterface{    private $container;    private $stagingManager;    public function setContainer(ContainerInterface $container = null)    {        $this->container = $container;        $this->stagingManager = $this->container->get('doctrine')->getManager('staging');    }    public function load(ObjectManager $manager)    {        $this->loadCountry($manager, 'Austria');        $this->loadCountry($manager, 'Germany');        $this->loadCountry($manager, 'France');        $this->loadCountry($manager, 'Spain');        $this->loadCountry($manager, 'Great Britain');        $manager->flush();    }    protected function loadCountry(ObjectManager $manager, $countryName)    {        $country = new Country($countryName);        $cities = $this->stagingManager->createQueryBuilder()            ->select('c')            ->from('AcmeDemoBundle:City', 'c')            ->leftJoin('c.country', 'co')            ->where('co.name = :country')            ->setParameter('country', $countryName)            ->getQuery()            ->getResult();        foreach ($cities as $city) {            $city->setCountry($country);            $manager->persist($city);        }        $manager->persist($country);    }}

What I did in the loadCountry method was that I load the objects from the staging entity manager, add a reference to the fixture country (the one that already exists in your current fixtures) and persist it using the default entity manager (your development database).

Sources: