Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 execute SQL file in Doctrine Fixtures Load

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.

like image 616
Pablo Avatar asked Dec 16 '14 19:12

Pablo


People also ask

What are fixtures in Symfony?

Fixtures are used to load a "fake" set of data into a database that can then be used for testing or to help give you some interesting data while you're developing your application. This bundle is compatible with any database supported by Doctrine ORM (MySQL, PostgreSQL, SQLite, etc.).


1 Answers

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;
}
like image 109
Pablo Avatar answered Sep 24 '22 23:09

Pablo