DBDesignerのXMLからsymfony(Propel)のschemaをつくる
XSLを使ってDBDesignerが出力するXMLからPropelのshema.xmlに変換するのをsymfonyのタスクに組み込んで使う方法。
まず、このXSLをconfig以下とかに置きます。
ちなみにこれはPropelのリポジトリにあがっているdbd2propelのXSLを僕がちょっといじったもの。onDeleteとかonUpdateまわりの挙動が微妙にいけてなかったので修正してます。
んで、DBDesignerのXMLをdata/xml以下あたりにdatabase.xmlとか名前つけて出力します。symfonyのタスクはこんな感じ。
/path/to/symfony_root/lib/task/propelDbdtoschemaTask.class.php
<?php
class propelDbdtoschemaTask extends sfBaseTask
{
protected function configure()
{
$this->addOptions(array(
new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name'),
new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
new sfCommandOption('connection', null, sfCommandOption::PARAMETER_REQUIRED, 'The connection name', 'propel'),
));
$this->namespace = 'propel';
$this->name = 'dbd-to-schema';
$this->briefDescription = 'DBDesigner4 xml to propel schema.xml';
$this->detailedDescription = <<<EOF
The [propel:dbd-to-schema|INFO] task does things.
Call it with:
[php symfony propel:dbd-to-schema|INFO]
EOF;
}
protected function execute($arguments = array(), $options = array())
{
$this->logSection('propel', 'Running dbd-to-schema task');
// initialize the database connection
$databaseManager = new sfDatabaseManager($this->configuration);
$connection = $databaseManager->getDatabase($options['connection'] ? $options['connection'] : null)->getConnection();
// file path setting
$xslFile = sfConfig::get('sf_config_dir') . '/dbd2propel.xsl';
$dbdXmlFile = sfConfig::get('sf_data_dir') . '/xml/database.xml';
$schemaFile = sfConfig::get('sf_config_dir') . '/schema.xml';
// load the DB Designer 4 XML
$xml = new DOMDocument;
$xml->load($dbdXmlFile);
// load the transformation stylesheet
$xsl = new DOMDocument;
$xsl->load($xslFile);
$proc = new XSLTProcessor();
// attach the xsl rules
$proc->importStyleSheet($xsl);
$schemaXml = $proc->transformToXML($xml);
file_put_contents($schemaFile, $schemaXml);
}
}
んで、
$ ./symfony propel:dbd-to-schema
これでconfig以下にschema.xmlが作られます。便利。XSLとかXMLのディレクトリは「file path setting」のところでセットしてるのでお好みで。
- Prev Entry:telnetでSMTPサーバーの動作確認
- Next Entry:sfFormの汚染された値とクリーンな値について