We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22303 MODX Staff
    • 10,725 Posts
    Quote from: rf9 at Jun 07, 2010, 04:07 PM

    Thanks OpenGeek, the 3PC stuff makes sense as does the elements and snippets structure. I’ve made a transport package before so I’m a little familiar with this layout.

    I still can’t get my head around the ’StoreFinder’ snippet. As I understand, it’s a temporary snippet for development. It allows you to develop in a different directory to your MODx install. Should it be placed in the MODx install or in the directory where you are developing the component?
    Oh I see. No, the first code in that section is the helper snippet code for development which you actually create a new Snippet for and copy and paste into the MODx manager. It will load the PHP file that is discussed in the remaining code samples in that section so you don’t have to keep copy and pasting the actual snippet code into the manager as you are developing it.
      • 34120
      • 236 Posts
      I think I’ve made some progress with this, well I have something working anyway. It’s been a struggle and I was close to leaving it and putting it on the backburner. I’ve cobbled something together myself using bits from the 3PC tutorial, various forum posts and the help from this thread. I got a bit stumped with the 3PC tutorial and towards the end I realised it was for generating a table, I was wanting to reverse engineer the schema from an existing database table.

      I’ll post my code below for anyone who is interested. It’s probably a bit crude so if any pro MODxers have a spare minute any suggestions or improvements would be welcome. I’ve also recently discovered the xPDO sub-forum, maybe this would have been more fitting over there.

      Here’s my code:

      dwcategories/_build/build.schema.php
      <?php// Using xPDO to reverse engineer existing database table (or something like that, hopefully)
      $mtime = microtime();  
      $mtime = explode(" ", $mtime);  
      $mtime = $mtime[1] + $mtime[0];  
      $tstart = $mtime;  
      set_time_limit(0); 
      
      require_once dirname(__FILE__) . '/build.config.php';
      include_once MODX_CORE_PATH . 'model/modx/modx.class.php';
      include_once XPDO_CORE_PATH . 'xpdo.class.php';
      
      // create the modx object and load the modPackageBuilder class
      $modx = new modX();
      $modx->initialize('mgr');
      $modx->loadClass('transport.modPackageBuilder','',false, true);
      $modx->setLogLevel(modX::LOG_LEVEL_INFO);  
      $modx->setLogTarget(XPDO_CLI_MODE ? 'ECHO' : 'HTML');  
      
      $root = dirname(dirname(__FILE__)).'/';  
      $sources = array(  
          'root' => $root,  
          'core' => $root.'core/components/dwcatalogue/',  
          'model' => $root.'core/components/dwcatalogue/model/',  
          'assets' => $root.'assets/components/dwcatalogue/',  
          'schema' => $root.'_build/schema/',  
      );
      
      $xpdo= new xPDO('mysql:host=localhost;dbname=database','username','password','dwcat_');
      
      // Set the package name and root path of that package
      $xpdo->setPackage('dwcatalogue', XPDO_CORE_PATH . '../model/');
      $xpdo->setDebug(true);
      $manager= $xpdo->getManager();
      $generator= $manager->getGenerator();
      
      //Use this to create a schema from an existing database
      $xml= $generator->writeSchema($sources['schema'].'dwcatalogue.mysql.schema.xml', 'dwcatalogue', 'xPDOSimpleObject', 'dwcat_');
      
      //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($sources['schema'].'dwcatalogue.mysql.schema.xml', $sources['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();?>
      This generates my schema and model files.

      Then I have a snippet: dwcategories/core/components/dwcatalogue/snippet.dwcatalogue.php (there’s possibly some unnecessary code repetition in here, not sure)
      <?php 
      require_once dirname(dirname(dirname(dirname(__FILE__)))) . '/_build/build.config.php';
      include_once MODX_CORE_PATH . 'model/modx/modx.class.php';
      include_once XPDO_CORE_PATH . 'xpdo.class.php';
      $root = dirname(dirname(__FILE__)).'/';  
      $sources = array(  
          'core' => $root.'dwcatalogue/',  
          'model' => $root.'dwcatalogue/model/',  
      );
      
      $xpdo= new xPDO('mysql:host=localhost;dbname=database','username','password','dwcat_');
      //$xpdo->setDebug(true);
      $xpdo->addPackage('dwcatalogue',$sources['model'],'dwcat_');  
      
      $rows = $xpdo->getCollection('Products',array(  
        'id' > 50,  
      ));  
      foreach ($rows as $row) {  
         echo '<p>Row - ' . $row->get('id') . ' - ' . $row->get('code'). ' - ' . $row->get('name')."</p>";  
      } ?>

      So it’s working, the snippet outputs the rows from my custom database table. Sorry for the long post, I’m going to move on with this now and try and work it in to my project.