2014-12-16 13 views
10

Sto migrando una vecchia app Web basata su SQL Server e ASP a Symfony2 e MySQL. Ho fatto alcune domande ed esportato vecchi dati in singoli file SQL. Come posso eseguire thoses file nei miei infissi, quando eseguire il comandoSymfony2 esegue il file SQL in Doctrine Fixtures Load

$php app/console doctrine:fixtures:load 

Ora ho alcuni dispositivi che lavora direttamente con Dottrina ORM e le entità, ma ho un sacco di dati da importare.

risposta

11

trovo una buona soluzione. Non ho trovato un metodo exec nella classe ObjectManager, quindi ... questo funziona molto bene per 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 questa soluzione la classe apparecchio deve attuare pienamente il ContainerAwareInterface con il metodo

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

utilizzare Symfony \ Component \ Finder \ Finder; – Nicodemuz

11

È possibile caricare il contenuto del file come una stringa, ed eseguire SQL nativo utilizzando l'EntityManager:

class SQLFixtures extends AbstractFixture implements OrderedFixtureInterface 
{  
    $filename = '/path/to/sql/file.sql'; 

    public function load(ObjectManager $manager) { 
    $sql = file_get_contents($filename); // Read file contents 
    $manager->getConnection()->exec($sql); // Execute native SQL 

    $manager->flush(); 
    } 

    public function getOrder() { 
    return 99; // Order in which this fixture will be executed 
    } 
} 
0

risposta per Zend Framework 2.5.3 utilizzando Doctrine Data-Fixtures.

Non sicuro se questo si applica alle risposte date, ma stanno provando un po 'troppo difficile. Se ispezionate l'oggetto $manager specificato, troverete che è già lo EntityManager (di interface ObjectManager) (almeno in ZF2). Come tale si è in grado di ottenere il Connection direttamente ed è possibile eseguire senza utilizzare $this->container->get('doctrine.orm.entity_manager')

Di seguito un frammento che uso per creare la prima "sistema" utente, con un FK di riferimento createdBy a se stessa.

public function load(ObjectManager $manager) 
{ 
    $sql = 'INSERT INTO users (
       id, username, email, display_name, `password`, created_by) 
      VALUES (:id, :username, :email, :display_name, :password, :created_by)'; 

    $password = $this->createSuperDuperEncryptedPassword(); 

    // $manager === `EntityManager|ObjectManager`, `->getConnection()` is available 
    $stmt = $manager->getConnection()->prepare($sql); 

    $stmt->bindValue(':id', 1); 
    $stmt->bindValue(':username', 'system'); 
    $stmt->bindValue(':email', '[email protected]'); 
    $stmt->bindValue(':display_name', 'system'); 
    $stmt->bindValue(':password', password); 
    $stmt->bindValue(':created_by', 1);  // Self reference 
    $stmt->execute(); 
} 
Problemi correlati