Symfony2在Doctrine Fixtures Load中执行SQL文件 [英] Symfony2 execute SQL file in Doctrine Fixtures Load

查看:333
本文介绍了Symfony2在Doctrine Fixtures Load中执行SQL文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将基于SQL Server和ASP的旧网络应用程序迁移到Symfony2和MySQL。我做了一些查询并将旧数据导出到单个SQL文件。
当我运行命令

  $ php app / console doctrine时,如何执行我的装置中的文件?装备:加载

现在我有一些工具直接与Doctrine ORM和实体工作,但我有一个很多要导入的数据。

解决方案

我找到了一个很好的解决方案。我在类 ObjectManager 中找不到一个 exec 方法,所以...这个工作对我来说很好/ p>

  public function load(ObjectManager $ manager)
{
//捆绑管理文件和目录
$ 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();
}
}

在这个解决方案中,你的夹具类必须实现 ContainerAwareInterface 与方法

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


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.

解决方案

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;
}

这篇关于Symfony2在Doctrine Fixtures Load中执行SQL文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆