如何检查表名在 Propel 中是否有效? [英] How do I check if table names are valid in Propel?

查看:57
本文介绍了如何检查表名在 Propel 中是否有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查一个表名在 Propel 中是否有效,然后用它做一些事情,比如获取它的 PHP 名称.问题是我正在使用 DatabaseMap 并且它只包含已实例化的表.例如:

I want to check if a table name is valid in Propel, and then do something with it such as get its PHP name. The problem is that I am using the DatabaseMap and it only contains tables that have been instantiated. For instance:

$map = Propel::getDatabaseMap();
$map->getTableName('group'); // throws exception

如果我遍历表:

$tables = $map->getTables();
foreach ($tables as $key => $value) {
    echo $key;
}

它只输出用户".它不会输出我数据库中的任何其他表.

It only outputs 'user'. It does not output any other tables in my database.

是否有其他方法可以检查 Propel 中的表名是否有效?

Are there any other methods to check if a table name is valid in Propel?

推荐答案

您可以使用 PHP 的 simplexml 解析您的 schema.xml.

You can parse your schema.xml with PHP's simplexml.

$xml = simplexml_load_file('schema.xml');
$tableExists = 0 < count($xml->xpath("table[@phpName='$tableName']"));

不要忘记为 $tableName 过滤用户的输入,否则有可能将自己的查询注入到 xpath 中.为了获得更好的性能,您应该缓存您的结果.

Don't forget to filter users's input for $tableName, otherwise it's possible to inject own query into xpath. To have better performance you should cache your results.

如果您根据您的 schema.xml 创建包含所有表的哈希映射,则更好,缓存此哈希映射并每次都进行检查.

Even better would be if you create a hash map with all tables based on your schema.xml, cache this hash map and check against this every time.

$hashMap = $foo->getCache('tables');

if (!$hashMap) {
    $xml = simplexml_load_file('schema.xml');
    $tables = $xml->xpath("table");
    foreach ($tables as $table) {
        $hashMap[$table['phpName']] = true;
    }
    $foo->setCache('tables', $hashMap);
}

$tableExists = isset($hashMap[$tableName]);

在这种情况下,实际上没有必要过滤用户的输入.

In this case it's not really necessary to filter user's input.

这篇关于如何检查表名在 Propel 中是否有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆