【问题标题】:Propel - retrieve all tables推进 - 检索所有表
【发布时间】:2016-06-10 23:07:45
【问题描述】:

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

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

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

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

更新

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

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

【问题讨论】:

  • 它会产生一个空数组,因为您需要事先初始化所有 TableMap,如果您有很多且内存不足,这将非常慢。更好地解析 .schema.xml

标签: php oop orm propel


【解决方案1】:

当前版本 2 中无法通过一次调用检索所有表映射。原因:我们需要加载所有表映射,这非常慢,而且我们没有总的“映射”/“数组”列表以及构建时可用的所有可用表映射。然而,在 Propel3 中,这是可能的。

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

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

【讨论】:

  • 似乎没有其他选择。在未来的版本中是否提供了类似的内容?
【解决方案2】:

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

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

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

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

【讨论】:

    【解决方案3】:

    你可以试试这个:

    <?php
    use Propel\Runtime\Propel;
    
    $map = Propel::getServiceContainer()->getDatabaseMap('default');
    $tables = $map->getTables();
    

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

    【讨论】:

      猜你喜欢
      • 2015-01-16
      • 2012-12-10
      • 2010-10-31
      • 2018-05-24
      • 2022-01-18
      • 2012-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多