Propel ORM-如何指定要进行反向工程的表



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

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

扩展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>

扩展属性架构反向任务:

类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

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

看看MysqlSchemaParser.php,了解这样一个解析器是如何工作的。您可以扩展这样一个现有的解析器,只需重写parse()方法。添加表时,您会检查它们是否排除/包含,只有在满足子集条件时才添加。

propel食谱中介绍了如何创建自定义推进任务。

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

如果做得巧妙,我会发现它值得作为对Propel项目的贡献,你肯定会因此而出名!:-)

最新更新