Example #1
0
 /**
  * @param $modx
  * @param $config - array() of config options like array('corePath'=>'Path.../core/)
  */
 function __construct(modX &$modx, $scriptProperties)
 {
     $this->modx =& $modx;
     $this->scriptProperties = $scriptProperties;
     /* load data packages */
     $this->config['package'] = $this->modx->getOption('package', $scriptProperties, '');
     $this->config['packagePath'] = MODX_CORE_PATH . $this->modx->getOption('packagePath', $scriptProperties, '');
     $this->config['lexicon'] = $this->modx->getOption('lexicon', $scriptProperties, $this->config['package'] . ':default');
     $this->classKey = $this->modx->getOption('classKey', $scriptProperties, '');
     // connect to foreign db:
     $this->foreign = (bool) $this->modx->getOption('foreign', $scriptProperties, FALSE);
     if ($this->foreign) {
         $this->db_object = 'foreignDB';
         require 'foreignconnect.class.php';
         $prefix = '';
         $config_file = $this->modx->getOption('configFile', $scriptProperties, NULL);
         if (!empty($config_file)) {
             require $config_file;
         } else {
             $db_type = $this->modx->getOption('dbType', $scriptProperties, 'mysql');
             $db_server = $this->modx->getOption('dbServer', $scriptProperties, 'localhost');
             $db_charset = $this->modx->getOption('dbCharset', $scriptProperties, 'utf8');
             $db_name = $this->modx->getOption('dbName', $scriptProperties, 'modx');
             $db_user = $this->modx->getOption('dbUser', $scriptProperties, 'modx_user');
             $db_password = $this->modx->getOption('dbPassword', $scriptProperties, 'password');
             $prefix = $this->modx->getOption('tablePrefix', $scriptProperties, '');
             $db_dsn = $db_type . ':host=' . $db_server . ';dbname=' . $db_name . ';charset=' . $db_charset;
         }
         // $this->modx->log(modX::LOG_LEVEL_ERROR,'DSN: '.$db_dsn);
         $this->foreignDB = ForeignConnect::getInstance($db_dsn, $db_user, $db_password);
         // returns an xPDO instance
         //echo ($this->foreignDB->connect()) ? 'Connected' : 'Not Connected';
     }
     // load package manually:
     $db_object = $this->db_object;
     if ($this->foreign) {
         if (!$this->foreignDB->addPackage('website_db', $this->config['packagePath'], '')) {
             //echo '<br>Could not load package: '.$this->config['package'].' - '.$this->config['packagePath'];
         }
         //echo '<br>Loaded package: '.$this->config['package'].' - '.$this->config['packagePath'].' with Prefix: '.$prefix;
         //$tmp = $this->foreignDB->newObject($this->classKey);
         //$tmp->fromArray(array());
     } else {
         $this->modx->addPackage($this->config['package'], $this->config['packagePath']);
     }
     // $this->modx->addPackage($this->config['package'], $this->config['packagePath']);
     if ($this->modx->lexicon) {
         $this->modx->lexicon->load($this->config['lexicon']);
     }
     $pk = 'id';
     $item = $this->{$db_object}->newObject($this->classKey);
     if (is_object($item)) {
         $pk = $item->getPK();
     }
     $this->primaryKey = $this->modx->getOption('primaryKey', $scriptProperties, $pk);
     //echo 'PK: '.$pk;
 }
<?php

/**
 * mytest table
 */
$limit = $modx->getOption('limit', $scriptProperties, 20);
require $modx->getOption('core_path') . '/config/bc_config.inc.php';
$output = '';
// this is what the snippet will return
/*$foreignDB = new xPDO('mysql:host=' . $foreign_database_host.';dbname='.$foreign_database_name/*.';charset='.$foreign_database_charset* /,
  $foreign_database_username,
  $foreign_database_password );*/
$package_path = $modx->getOption('core_path') . 'components/foreigndb/model/';
require_once $package_path . 'foreignconnect.class.php';
$foreignDB = ForeignConnect::getInstance($database_dsn, $database_user, $database_password);
// see the scheme file and the xml model element and you will see the attribute package and that must match here
// make sure you set the prefix as empty if you don't use it
if (!$foreignDB->addPackage('foreigndb', $package_path, '')) {
    return 'Can not load package';
}
// lets add some data!
// see the scheme file and the xml object element and you will see the attribute class and that must match here
/*
$myRow = $foreignDB->newObject('EventName');
$data = array(
        'name' => 'MODX Revolution',
        'description' => 'A great CMS product...'
    );
$myRow->fromArray($data);

if ( !$myRow->save() ) {
Example #3
0
 /**
  * Close the instance
  */
 public function close()
 {
     self::$instance = array();
 }