Propel - 检索所有表

Propel - retrieve all tables

我想使用 propel 输出所有模式 table。当引用特定的 TableMap

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

有没有办法 而不是 使用特定的 table(例如 Users)但有更通用的方法?有一个有点过时的question here面临同样的问题。

我应该进行自定义查询还是解析 schema.xml 以检索所有 table?

更新

下面作为答案给出的一些解决方案产生空数组

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

你可以试试这个:

<?php
use Propel\Runtime\Propel;

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

default 应替换为您为数据库连接定义的任何名称。

可以通过获取当前的propel服务容器来访问db map,例如:

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

具体来说,上面的代码将从默认连接中提取数据库映射,但您可以指定配置的另一个连接,假设您有一个名为 "asdrubale":

的辅助连接
$dbMap = Propel::getServiceContainer()->getDatabaseMap('asdrubale');
$tables = $dbMap->getTables();

当前版本 2 无法一次调用检索所有 table 地图。原因:我们需要加载所有 table 地图,这非常慢,而且我们没有完整的 "map"/"array" 列表,其中包含构建时可用的所有可用 table 地图。然而,在 Propel3 中,这是可能的。

您应该遵循的唯一解决方案是解析 schema.xml:How do I check if table names are valid in Propel?

你还可以做的是使用 Propel 的反向 类 来反向真实的数据库。但是,这非常慢,因为它会一直读取整个数据库结构。参见 https://github.com/propelorm/Propel2/issues/261#issuecomment-40659647