/**
* Connect to different database
*/
public function connect($configFile = '') {
$configFile = !empty($configFile) ? $configFile : 'dbconnect.php';
$db = require_once $configFile;
$dsn = 'mysql:host=' . $db['host'] . ';dbname=' . $db['dbName'] . ';port=' . $db['port'] . ';charset=utf8';
$connect = $this->modx->addConnection($dsn, $db['username'], $db['password']);
if ($connect) {
$this->modx->addPackage('mypackage', $this->config['modelPath'], '');
}
}
<model package="mypackage" database="different_database" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1">
/**
* Connect to different database
*/
public function connect() {
if (!$this->_connect) {
$db = require_once 'dbconnect.php';
$dsn = 'mysql:host=' . $db['host'] . ';dbname=' . $db['dbName'] . ';port=' . $db['port'] . ';charset=utf8';
$this->_connect = new xPDO($dsn, $db['username'], $db['password']);
// $this->modx->log(modX::LOG_LEVEL_ERROR, __LINE__ . ': connect() ' . $this->_connect->connect() ? 'Connected </br>' : 'Not Connected');
$this->_connect->setPackage('my_package', $this->config['modelPath']);
}
return $this->_connect;
}
<?php
class MembersGetListProcessor extends modObjectGetListProcessor {
/** @var string $defaultSortField The default field to sort by */
public $defaultSortField = 'id';
/** @var string $defaultSortDirection The default direction to sort */
public $defaultSortDirection = 'ASC';
/**
* Get the data of the query
* @return array
*/
public function getData() {
$data = array();
$limit = intval($this->getProperty('limit'));
$start = intval($this->getProperty('start'));
$c = $this->modx->myClass->connect()->newQuery($this->classKey);
$c = $this->prepareQueryBeforeCount($c);
$data['total'] = $this->modx->myClass->connect()->getCount($this->classKey,$c);
$c = $this->prepareQueryAfterCount($c);
$sortClassKey = $this->getSortClassKey();
$sortKey = $this->modx->myClass->connect()->getSelectColumns($sortClassKey,$this->getProperty('sortAlias',$sortClassKey),'',array($this->getProperty('sort')));
if (empty($sortKey)) $sortKey = $this->getProperty('sort');
$c->sortby($sortKey,$this->getProperty('dir'));
if ($limit > 0) {
$c->limit($limit,$start);
}
$data['results'] = $this->modx->myClass->connect()->getCollection($this->classKey,$c);
return $data;
}
}
return 'MembersGetListProcessor';