1

我有一个包含几十个表的数据库。我的应用程序只处理其中一张表。我想推动为该表生成模式。有没有办法可以在 build.properties 中指定表?

4

2 回答 2

2

Propel 的简单扩展(基于 Propel-1.6.8 的代码示例)允许忽略一组自定义表(使用属性配置):

扩展 build-propel.xml:

<!-- Ignore Tables Feature -->
<taskdef
name="propel-schema-reverse-ignore"
classname="task.PropelSchemaReverseIgnoreTask" classpathRef="propelclasses"/>

扩展 build.xml:

<target name="reverse-ignore" depends="configure">
   <phing phingfile="build-propel.xml" target="reverse-ignore"/>
</target>

扩展 PropelSchemaReverseTask:

类 PropelSchemaReverseIgnoreTask 扩展 PropelSchemaReverseTask {

/**
 * Builds the model classes from the database schema.
 * @return Database The built-out Database (with all tables, etc.)
 */
protected function buildModel()
{
    // ...

    // Loads Classname reverse.customParserClass if present
    $customParserClassname = $config->getBuildProperty("reverseCustomParserClass");
    if ($customParserClassname!=null) {
        $this->log('Using custom parser class: '.$customParserClassname);
        $parser = $config->getConfiguredSchemaParserForClassname($con, $customParserClassname);
    } else {
        $parser = $config->getConfiguredSchemaParser($con);
    }

    // ...
}

}

向 GeneratorConfig 添加函数:

class GeneratorConfig implements GeneratorConfigInterface {
// ...
/**
 * Ignore Tables Feature:
 * Load a specific SchemaParser class
 * 
 * @param PDO $con
 * @param string $clazzName SchemaParser class to load
 * @throws BuildException
 * @return Ambigous <SchemaParser, unknown>
 */
public function getConfiguredSchemaParserForClassname(PDO $con = null, $clazzName)
{
    $parser = new $clazzName();
    if (!$parser instanceof SchemaParser) {
        throw new BuildException("Specified platform class ($clazz) does implement SchemaParser interface.", $this->getLocation());
    }
    $parser->setConnection($con);
    $parser->setMigrationTable($this->getBuildProperty('migrationTable'));
    $parser->setGeneratorConfig($this);

    return $parser;
}

}

扩展 MysqlSchemaParser:

class MysqlSchemaIgnoreParser extends MysqlSchemaParser {

public function parse(Database $database, Task $task = null)
{
   $this->addVendorInfo = $this->getGeneratorConfig()->getBuildProperty('addVendorInfo');

    $stmt = $this->dbh->query("SHOW FULL TABLES");

    // First load the tables (important that this happen before filling out details of tables)
    $tables = array();

    $configIgnoreTables = $this->getGeneratorConfig()->getBuildProperty("reverseIgnoreTables");

    $tables_ignore_list = array();
    $tables_ignore_list = explode(",", $configIgnoreTables);

    if ($task) {
        $task->log("Reverse Engineering Tables", Project::MSG_VERBOSE);
    }

    while ($row = $stmt->fetch(PDO::FETCH_NUM)) {
        $name = $row[0];
        $type = $row[1];

        if (!in_array($name, $tables_ignore_list)) {

            // ... 

        } else {
            $task->log("Ignoring table: " .$name);
        }
    }

    // ...
}

}

向 build.properties 添加新的(可选)属性:

# Propel Reverse Custom Properties
propel.reverse.customParserClass=MysqlSchemaIgnoreParser
propel.reverse.ignoreTables = table1,table2
于 2015-01-27T15:23:52.773 回答
1

一个解决方案可能是编写您自己的架构解析器来忽略某些表。这些要排除的表可以从配置文件中读取。

查看MysqlSchemaParser.php以了解此类解析器的工作原理。您可以扩展这样一个现有的解析器并覆盖该parse()方法。添加表时,您将检查它们是否排除/包含,并且仅在它们满足您的子集标准时才添加它们。

Propel 食谱中描述了如何创建自定义推进任务。

propel-gen reverse然后,您将调用自定义的逆向工程任务,而不是调用。

如果做得好,我会发现它值得被添加为 Propel 项目的贡献,你肯定会因此而声名鹊起!:-)

于 2013-12-13T17:38:38.237 回答