推动ORM - 如何指定表格进行逆向工程?

时间:2013-08-12 18:34:05

标签: schema reverse propel

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

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

看一下MysqlSchemaParser.php,了解这样的解析器是如何工作的。您可以扩展此类现有解析器,只需覆盖parse()方法即可。添加表格后,您可以检查它们是否有排除/包含,只有在符合子集标准时才添加它们。

如何创建自定义推进任务是described in the Propel cookbook

您可以调用自定义的逆向工程任务,而不是调用propel-gen reverse

如果做得很整齐,我会发现值得加入作为对Propel项目的贡献,你肯定应该为此成名! : - )