简体   繁体   中英

Propel - retrieve all tables

I would like to have an output of all schema tables using propel. When referencing to a particular TableMap

$users = UsersTableMap::getTableMap();
$map = $users->getDatabaseMap();
$tables = $map->getTables(); //yields an object, holds only Users table

Is there a way not to use a particular table (eg Users ) but have a more general approach? There is a bit outdated question here that faces the same problem.

Should I make a custom query or parse the schema.xml to retrieve all tables?

Update

Some of the solutions given below as answers produce empty array

$map = Propel::getServiceContainer()->getDatabaseMap(); //w & w/o string argument
$tables = $map->getTables(); //an empty array

There's no way in the current version 2 to retrieve all table maps with one call. Why: We would need to load all table maps which is incredible slow and we have no total "map"/"array" listing with all available table maps that was available at buildtime. In Propel3 however, this is possible.

The only solution you should follow is parsing the schema.xml: How do I check if table names are valid in Propel?

What you can additional do is to reverse the real database using the reverse classes of Propel. However, this is very slow as it will read all the time the whole database structure. See https://github.com/propelorm/Propel2/issues/261#issuecomment-40659647

You could try this:

<?php
use Propel\Runtime\Propel;

$map = Propel::getServiceContainer()->getDatabaseMap('default');
$tables = $map->getTables();

default should be replaced with whatever name you defined for your database connection.

you can access the db map by obtaining the current propel service container, eg:

$dbMap = Propel::getServiceContainer()->getDatabaseMap();
$tables = $dbMap->getTables();

Specifically the code abowe will extract the db map from the default connection, but you can specify another connection configured, let's assume you have a secondary connection called "asdrubale":

$dbMap = Propel::getServiceContainer()->getDatabaseMap('asdrubale');
$tables = $dbMap->getTables();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM