Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 - Show all tables from database

Tags:

php

mysql

symfony

I would like to retrieve all the tables of my database as a list.

i tried to do a "Show databases" on a query but as i'm not using a class I defined (entity) in symfony it's not working.

And with DQL :

   $em = $this->getDoctrine()->getEntityManager();
   $query = $em->createQuery(
        'show databases');

    $result = $query->getResult();

This error :

[Syntax Error] line 0, col 0: Error: Expected SELECT, UPDATE or DELETE, got 'show'

Any idea to help me ?

like image 583
hyptos Avatar asked Dec 27 '12 15:12

hyptos


1 Answers

As mentioned in a different answer, you can use Doctrine\DBAL for that:

/** @type \Doctrine\DBAL\Connection $connection */
$connection = ...;

/** @type \Doctrine\DBAL\Schema\MySqlSchemaManager $sm */
$sm = $connection->getSchemaManager();

And then just list the tables as Array:

var_dump( $sm->listDatabases() );
like image 95
kaiser Avatar answered Oct 14 '22 11:10

kaiser