Symfony2 execute SQL file in Doctrine Fixtures Load

Pablo picture Pablo · Dec 16, 2014 · Viewed 13.2k times · Source

I'm migrating an old web app based on SQL Server and ASP to Symfony2 and MySQL. I made some queries and export old data to individual SQL files. How can I execute thoses files in my fixtures, when I run the command

$php app/console doctrine:fixtures:load

Now I have some fixtures that works directly with Doctrine ORM and entities, but I have a lot of data to import.

Answer

Pablo picture Pablo · Dec 17, 2014

I find a good solution. I didn't find an exec method in class ObjectManager, so... this work very well for me.

public function load(ObjectManager $manager)
{
    // Bundle to manage file and directories
    $finder = new Finder();
    $finder->in('web/sql');
    $finder->name('categories.sql');

    foreach( $finder as $file ){
        $content = $file->getContents();

        $stmt = $this->container->get('doctrine.orm.entity_manager')->getConnection()->prepare($content);
        $stmt->execute();
    }
}

In this solution your fixture class has to implement the ContainerAwareInterface with the method

public function setContainer( ContainerInterface $container = null )
{
    $this->container = $container;
}