We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28376
    • 222 Posts
    I just downloaded xPDO-2.0.1 I couldn’t find a getting started where you include this class in your main php document. Basically what I want to use xPDO is writing to the pre-defined database and requesting data to a pre-defined database.

      • 32674
      • 101 Posts
      Kinda hidden in the site, it’s labeled ’wiki’ on the site’s links, not ’docs’.
      http://rtfm.modx.com/display/xPDO20/Getting+Started

      To get rolling using xPDO as a standalone w/o Modx, you will have to first generate your classes that model your database (one time deal for steps 1-3):

      1. generate the xml schemas from the db. (assumes your db is prefixed with ’mydb_’)
         include_once ('/xpdo/xpdo.class.php');
         $xpdo= new xPDO('mysql:host=localhost;dbname=myolddatabase','username','password','mydb_');  
         $manager= $xpdo->getManager();  
         $generator= $manager->getGenerator();  
         
         $xml= $generator->writeSchema('/path/to/my/new/directory_name.schema.xml','directory_name', 'xPDOObject','mydb_');


      2. Add in the table relations to your xml schemas by hand: http://rtfm.modx.com/display/xPDO20/Defining+Relationships

      3. Next, generate the php classes from the schemas + relationships
         $schema = '/path/to/my/new/directory_name.schema.xml';  
         $target = '/path/to/directory/holding/my/generated/db/classes/';  
         $generator->parseSchema($schema,$target);  



      Its all detailed here: http://rtfm.modx.com/display/xPDO20/Creating+a+Model+With+xPDO

      --------------

      Then, to use the classes youve generated above, do something like this (sample snagged from: core/model/schema/build.modx.php):

      Here’s the main class to include: xpdo\xpdo.class.php
      include_once ('/xpdo/xpdo.class.php');
      
      //Set some valid connection properties here
      $xpdo= new xPDO( DSN, DB_USER, DB_PASS, XPDO_TABLE_PREFIX,
                      array (
                             PDO::ATTR_ERRMODE => PDO::ERRMODE_WARNING,
                             PDO::ATTR_PERSISTENT => false,
                             PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true
                      )
      );
      // set path to your classes directory
      $xpdo->setPackage('modx', dirname(XPDO_CORE_PATH) . '/model/');
      $xpdo->setDebug(true);
      // do your crud activities here


      About the only non obvious line in there is the ’$xpdo setpackage’ call. A more obvious, but less concise, name for that might be ’SetDirectoryOfDbClassesLocation’:
      $xpdo->setPackage(directory_name_holding_generated_db_classes, path_to_parent_directory);


      Hope that gets you started.
        • 3749
        • 24,544 Posts
        You might find this helpful too, although it assumes that you’ve moved the tables into the MODx DB:

        http://bobsguides.com/custom-db-tables.html

        There’s also some info here about how MODx uses xPDO that might help get you started:

        http://bobsguides.com/revolution-objects.html
          Did I help you? Buy me a beer
          Get my Book: MODX:The Official Guide
          MODX info for everyone: http://bobsguides.com/modx.html
          My MODX Extras
          Bob's Guides is now hosted at A2 MODX Hosting
          • 9207 ☆ A M B ☆
          • 2,475 Posts
          There’s a reverse-engineering script too, which is a great place to start if you’ve already got a database set up:
          http://rtfm.modx.com/display/revolution20/Reverse+Engineer+xPDO+Classes+from+Existing+Database+Table