瀏覽代碼

Agregado del comando para exportar datos desde una tabla a un archivo yaml

Your Name 7 年之前
父節點
當前提交
d7cf6d9a3c
共有 1 個文件被更改,包括 68 次插入0 次删除
  1. 68 0
      Command/CreateYamlForImportCommand.php

+ 68 - 0
Command/CreateYamlForImportCommand.php

@@ -0,0 +1,68 @@
+<?php
+
+namespace MigrationsBundle\Command;
+
+use ClientBundle\Entity\Client;
+use MapBundle\Entity\Location;
+use MapBundle\Util\GeoDecode;
+use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
+use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputOption;
+use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Output\OutputInterface;
+
+class CreateYamlForImportCommand extends ContainerAwareCommand
+{
+    protected function configure()
+    {
+        $this
+            ->setName('migrate:create:import')
+            ->setAliases(array("ik:mg:import"))
+            ->setDescription('Create yaml for import.')
+            ->setHelp('Create yaml from table.')
+            ->addArgument('table', InputArgument::REQUIRED, "Table name to export data.")
+            ->addArgument('file-name', InputArgument::REQUIRED, "Name of the file.");
+    }
+
+    protected function execute(InputInterface $input, OutputInterface $output)
+    {
+        $em = $this->getContainer()->get('doctrine')->getManager();
+        $table = $input->getArgument('table');
+        $fileName = $input->getArgument('file-name');
+
+        $stmt = $em->getConnection()->executeQuery("select * from " . $table);
+        if ($stmt->rowCount() > 0) {
+            $result = $stmt->fetchAll();
+            $str = "insertorupdate:\n";
+            $str .= "  $table:\n";
+            foreach ($result as $row) {
+                $first = true;
+                foreach ($row as $key => $value) {
+                    $value = str_replace("\\", '\\\\', $value);
+                    $value = str_replace('"', '\"', $value);
+                    $value = str_replace(array("\n", "\r"), array("\\n", "\\r"), $value);
+                    if (trim($value) == "") {
+                        // al estar vacio el string, tengo que verificar si el valor es null
+                        if (isset($row['id'])) {
+                            // existe la columna id para utilizar en el where
+                            $stmtNull = $em->getConnection()->executeQuery("select if(isnull(" . $key . "), 1, 0) as valor from " . $table . " where id = " . $row['id']);
+                            if ($stmtNull->rowCount() > 0) {
+                                $resultNull = $stmtNull->fetchAll();
+                                $value = $resultNull[0]['valor'] == 1 ? "NULL" : "";
+                            }
+                        }
+                    }
+                    if ($first) {
+                        $str .= "    - $key: \"" . $value . "\"\n";
+                        $first = false;
+                    } else {
+                        $str .= "      $key: \"" . $value . "\"\n";
+                    }
+                }
+            }
+            file_put_contents($fileName, $str);
+        } else {
+            echo "No results";
+        }
+    }
+}