Symfony2在Doctrine Fixtures Load中执行SQL文件 [英] Symfony2 execute SQL file in Doctrine Fixtures Load
问题描述
当我运行命令
$ 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屋!