We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9207 ☆ A M B ☆
    • 2,475 Posts
    Looking at this: http://rtfm.modx.com/xpdo/2.x/getting-started/using-your-xpdo-model/database-connections-and-xpdo

    There is no example of how to issue queries to the "other" database. Has anyone used this feature? How can I issue a query to one database and then then issue another query to a different database?

    Relatedly, do these additional options relate in any way to the mystical $config_options and $driver_options arrays in the MODX config file?
      • 22303 MODX Staff
      • 10,725 Posts
      The feature is not meant to allow connecting to a specific database node, it is for configuring master/slave configurations where one (or more) nodes are read-only and at least one is writable (aka mutable). In that case you can request the initial connection be read-only and xPDO will automatically switch to a writable connection if a write operation is performed on a database object.
        • 11055 ☆ A M B ☆
        • 3,112 Posts
        Interesting, I'm having a similar challenge.

        I've been using a solution for many cases, but now I need to use modx's class based processor (modProcessor).

        This processor doesn't have a feature that can switch the alien database instance.
        It keeps trying to search the table inside the same database where modx stands.

        I've tried this, too:
            /**
             * 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'], '');
                }
            }
        

        but it didn't work.

        Am I missing something here?

        I wonder if it can work if the schema has the database name inside it, eg:
        <model package="mypackage" database="different_database" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1">

        hoping the xPDO can switch to different node.
          Rico
          Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
          MODx is great, but knowing how to use it well makes it perfect!

          www.virtudraft.com

          Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

          Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

          Maintainter/contributor of Babel

          Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.
          • 11055 ☆ A M B ☆
          • 3,112 Posts
          Lol...
          Ok, I solved my own case.
          To use modProcessor for different database, I just needed to override the API that tried to connect the database.

          I keep using my previous way to connect alien DB
              /**
               * 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;
              }
          


          This is my solution for the processor(s):
          <?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';


          IDK whether it solves your case, it does on mine.
            Rico
            Genius is one percent inspiration and ninety-nine percent perspiration. Thomas A. Edison
            MODx is great, but knowing how to use it well makes it perfect!

            www.virtudraft.com

            Security, security, security! | Indonesian MODx Forum | MODx Revo's cheatsheets | MODx Evo's cheatsheets

            Author of Easy 2 Gallery 1.4.x, PHPTidy, spieFeed, FileDownload R, Upload To Users CMP, Inherit Template TV, LexRating, ExerPlan, Lingua, virtuNewsletter, Grid Class Key, SmartTag, prevNext

            Maintainter/contributor of Babel

            Because it's hard to follow all topics on the forum, PING ME ON TWITTER @_goldsky if you need my help.