We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
Subscribe: RSS
  • Just wanted to share an example script for reverse-engineering a set of tables in a database to generate an xPDO schema. The schema is an XML file where your table meta-data is described. Once you have reverse-engineered the XML from the database, you would normally then proceed to describing the relationships between those tables using aggregate and composite relation elements in the schema.

    Here is the build script used to reverse-engineer ($generator->writeSchema()) and forward-engineer ($generator->parseSchema()) the MODx database for 0.9.7 as an example.
    $mtime= microtime();
    $mtime= explode(" ", $mtime);
    $mtime= $mtime[1] + $mtime[0];
    $tstart= $mtime;
    //Customize this line based on the location of your script
    include_once (strtr(realpath(dirname(__FILE__)) . '/../../xpdo/xpdo.class.php', '\\', '/'));
    $xpdo= new xPDO('mysql:host=localhost;dbname=modx','username','password','modx_');
    // Set the package name and root path of that package
    $xpdo->setPackage('modx', XPDO_CORE_PATH . '../model/');
    $manager= $xpdo->getManager();
    $generator= $manager->getGenerator();
    //Use this to create a schema from an existing database
    $xml= $generator->writeSchema(XPDO_CORE_PATH . '../model/schema/modx.mysql.schema.xml', 'modx', 'xPDOObject', 'modx_');
    //Use this to generate classes and maps from your schema
    // NOTE: by default, only maps are overwritten; delete class files if you want to regenerate classes
    //$generator->parseSchema(XPDO_CORE_PATH . '../model/schema/modx.mysql.schema.xml', XPDO_CORE_PATH . '../model/');
    $mtime= microtime();
    $mtime= explode(" ", $mtime);
    $mtime= $mtime[1] + $mtime[0];
    $tend= $mtime;
    $totalTime= ($tend - $tstart);
    $totalTime= sprintf("%2.4f s", $totalTime);
    echo "\nExecution time: {$totalTime}\n";
    exit ();