How to know if a SQL was executed correctly, Symfony 2?

2

I use the following code to run an SQL file in my symfony 2.7 application, and it works, but I want to know how to validate if the query ran correctly or not.

This is my code:

public function importarAction(Request $request)
{

    /**
     * Creo un formulario para Seleccionar el archivo SQL
     */
    $defaultData = array('message' => 'Type your message here');
    $form = $this->createFormBuilder($defaultData)
        ->add('archivo', 'file', array('label' => 'Seleccione el Archivo SQL: '))
        ->getForm();

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {

        /**
         * Obtengo la ruta del Archivo y lo Muevo hasta
         * mi propio directorio dentro de la Aplicacion symfony
         */
        $file= $form->get('archivo')->getData();
        $nombre='SQLimportado'.date("dmYHis", time()).'.sql';
        $cvDir = $this->container->getparameter('kernel.root_dir').'\Resources\importados\';
        $file->move($cvDir,$nombre);

        /**
         * Busco el archivo utilizando los datos anteriores
         * como la ruta y el nombre del archivo
         */
        $finder = new Finder();
        $finder->files()->in($cvDir);
        $finder->name($nombre);

        foreach ($finder as $file) {
            /**
             * Ejecuo el SQL
             */
            $contenido = $file->getContents();
            $em = $this->getDoctrine()->getEntityManager();
            $db = $em->getConnection()->executeQuery($contenido);

            var_dump($db->errorInfo());

        }
    }

    return array(
        'form'   => $form->createView(),
    );

}
    
asked by Yulian David 03.07.2016 в 07:22
source

1 answer

0

In the end I got a result:

What I did was execute the SQL file using mysqli, not with the Dcotrine Manager.

/**
 * @Route("/importar", name="importar")
 * @Template("SeguridadBundle:Default:index.html.twig")
 */
public function importarAction(Request $request)
{

    /**
     * Creo un formulario para Seleccionar el archivo SQL
     */
    $defaultData = array('message' => 'Type your message here');
    $form = $this->createFormBuilder($defaultData)
        ->add('archivo', 'file', array('label' => 'Seleccione el Archivo SQL: '))
        ->getForm();

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {

        /**
         * Obtengo la ruta del Archivo y lo Muevo hasta
         * mi propio directorio dentro de la Aplicacion symfony
         */
        $file= $form->get('archivo')->getData();
        $nombre='SQLimportado'.date("dmYHis", time()).'.sql';
        $cvDir = $this->container->getparameter('kernel.root_dir').'\Resources\importados\';
        $file->move($cvDir,$nombre);

        /**
         * Busco el archivo utilizando los datos anteriores
         * como la ruta y el nombre del archivo
         */
        $finder = new Finder();
        $finder->files()->in($cvDir);
        $finder->name($nombre);


        foreach ($finder as $file) {
            /**
             * Seobtiene el contenido del SQL
             * y se Crea la conexion a MySQL
             */
            $contenido = $file->getContents();
            $em = $this->getDoctrine()->getEntityManager();
            $parametros = $em->getConnection()->getParams();

            /**
             * Creo un nuevo parametro de Mysqli
             */
            $mysqli = new \mysqli(
                $parametros['host'],
                $parametros['user'],
                $parametros['password'],
                $parametros['dbname']);

            /**
             * Ejecutor el archivo SQL
             * mediante la opcion de, multiple query
             * y valido si existen errores durante su ejecucion
             */
            if ($mysqli->multi_query($contenido)) {
                do {

                    if (!$mysqli->more_results()) {
                        break;
                    }
                    if (!$mysqli->next_result()) {
                        /**
                         * si hay error de algun tipo Se Mostrara el mensaje de ERROR
                         */
                        return $this->render('SeguridadBundle:Default:respuesta.html.twig', array(
                            'resultado' => 'Seha producido un error durante la ejecución del SQL!',
                        ));
                        break;
                    }
                } while (true);
                /**
                 * si todo esta bien Se muestra el MSG de CORRECTO
                 */
                return $this->render('SeguridadBundle:Default:respuesta.html.twig', array(
                    'resultado' => 'La importación de ha ejecutado correctamente!',
                ));
            }
            else {
                /**
                 * si hay error de algun tipo Se Mostrara el mensaje de ERROR
                 */
                return $this->render('SeguridadBundle:Default:respuesta.html.twig', array(
                    'resultado' => 'Seha producido un error durante la ejecución del SQL!',
                    'error'=>$mysqli->errno
                ));

            }
        }
    }

    return array(
        'form'   => $form->createView(),
    );

}
    
answered by 04.07.2016 / 00:30
source