Pab*_*blo 16 mysql sql fixtures symfony doctrine-orm
我正在将基于SQL Server和ASP的旧Web应用程序迁移到Symfony2和MySQL.我做了一些查询并将旧数据导出到单个SQL文件.当我运行命令时,如何在我的灯具中执行thoses文件
$php app/console doctrine:fixtures:load
Run Code Online (Sandbox Code Playgroud)
现在我有一些直接与Doctrine ORM和实体一起工作的灯具,但我有很多数据需要导入.
Pab*_*blo 15
我找到一个很好的解决方案 我没有exec在课堂上找到一个方法ObjectManager,所以...这对我来说非常好.
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();
}
}
Run Code Online (Sandbox Code Playgroud)
在此解决方案中,fixture类必须ContainerAwareInterface使用该方法实现
public function setContainer( ContainerInterface $container = null )
{
$this->container = $container;
}
Run Code Online (Sandbox Code Playgroud)
Gui*_*rez 13
您可以将文件内容作为字符串加载,并使用EntityManager执行本机SQL:
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
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11323 次 |
| 最近记录: |