Last active
August 29, 2015 14:06
-
-
Save zaunaf/2043b7cc16ce2d17f461 to your computer and use it in GitHub Desktop.
XML Editing Using PHP SimpleXML - Test case with Propel's schema.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
try { | |
$projectDir = '/yourprojectfolder'; | |
$appName = 'MyApp'; | |
$schemaPath = $projectDir."/app/config/schema.xml"; | |
if (!is_file($schemaPath)) { | |
throw new Exception("File $schemaPath gagal dibuka, proses reverse gagal.."); | |
} | |
$schemaDesignObj = simplexml_load_file($schemaPath); | |
// Skip shit | |
// Let's assume that you hydrated a new schema from dbo, and no longer need | |
// to access them from the application either read or write | |
$skipSchemas = array("dbo", "sales"); | |
$skipTables = array("log_", "trigger_"); //skip machine purposed tables | |
$skipColumns = array("updated_by"); //skip automated columns | |
if (!$schemaDesignObj) { | |
throw new Exception("File $schemaPath gagal dimuat dalam XML Object"); | |
} | |
// Propel doesn't add this, so let's do the job | |
$schemaDesignObj->addAttribute("namespace", $appName."Model" ); | |
echo "Jumlah tabel sebelumnya : ".sizeof($schemaDesignObj->table)."<br>\r\n"; | |
$tablesToRemove = ""; | |
$columnsToRemove = ""; | |
foreach ($schemaDesignObj->table as $t) { | |
$schemaName = $t['schema'] == "" ? "dbo" : $t['schema']; | |
$tableName = $t['name']; | |
if (contains($schemaName, $skipSchemas)) { | |
echo "SKIPSCHEMA -- $schemaName<br>\r\n"; | |
$tablesToRemove[] = $t; | |
continue; | |
} | |
if (contains($tableName, $skipTables)) { | |
echo "SKIPTABLE -- $tableName<br>\r\n"; | |
$tablesToRemove[] = $t; | |
continue; | |
} | |
echo "Schema: ". $schemaName."; Table: ".$tableName."<br>\r\n"; | |
foreach ($t->column as $c) { | |
$columnName = $c['name']; | |
if (contains($columnName, $skipColumns)) { | |
echo "SKIPCOLUMN -- $columnName<br>\r\n"; | |
$columnsToRemove[] = $c; | |
continue; | |
} | |
echo " Column: ". $c['name']."<br>\r\n"; | |
} | |
} | |
// Remove the tables | |
foreach ($tablesToRemove as $t) { | |
unset ($t[0]); | |
} | |
// Remove the columns | |
foreach ($columnsToRemove as $c) { | |
unset ($c[0]); | |
} | |
echo "Jumlah tabel sesudahnya : ".sizeof($schemaDesignObj->table)."<br>rn";; | |
$schemaDesignStr = $schemaDesignObj->asXml(); | |
$target = $projectDir."/app/config/schema-design.xml"; | |
$fp = fopen($target, 'w'); | |
if (!$fp) { | |
throw new Exception("File $target gagal dibuka"); | |
} | |
if (!fwrite($fp, $schemaDesignStr)) { | |
throw new Exception("File $target gagal ditulisi"); | |
} | |
fclose($fp); | |
echo "<br>rnFile $target berhasil ditulis tanpa error."; | |
} catch (Exception $e) { | |
$status = 2; | |
$errMsg = $e->getMessage(); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment