We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 41594
    • 4 Posts
    Hi! I'm trying to create a component for MODX Revo using custom databases. I've managed to get xpdo working, now I'm trying to follow the Doodles tutorial. I've got stuck at the point when a processor appears. Here is the code from tutorial with my component name (psyab) instead of doodle:
    $corePath = $modx->getOption('psyab.core_path',null,$modx->getOption('core_path').'components/psyab/');
    
    require_once $corePath.'model/psyAb/psyab.class.php';
    
    $modx->addPackage('psyAb',$corePath.'model/');
    $modx->psyab = new psyAb($modx);
    
    $modx->lexicon->load('psyab:default');
    //echo $modx->lexicon('desc');
    /* handle request */
    $path = $modx->getOption('processorsPath',$modx->psyab->config,$corePath.'processors/');
    $modx->request->handleRequest(array(
        'processors_path' => $path,
        'location' => '',
    ));


    I know that in order to use custom db (component tables are in a different mysql database) I need to create an xpdo instance:
    $xpdo = new xPDO('mysql:host=localhost;dbname=dbname', 'login', 'pass', 'prefix_');
    


    Now, how can I make those two things work together?
      • 11055 ☆ A M B ☆
      • 3,112 Posts
      I usually add
      $modx->addPackage('psyAb',$corePath.'model/');

      inside the class's constructor, your case, the psyAb class.

      And remember, you should add the third argument if you use different prefix for the tables.
      $modx->addPackage('psyAb',$corePath.'model/', 'prefix_');


      Have you looked more docs further about using custom database?
      http://rtfm.modx.com/display/revolution20/Using+Custom+Database+Tables+in+your+3rd+Party+Components
      http://rtfm.modx.com/display/revolution20/Reverse+Engineer+xPDO+Classes+from+Existing+Database+Table
      or this
      https://forums.modx.com/thread/77038/instantiate-xpdo-from-different-database-solved#dis-post-425695
        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.
        • 41594
        • 4 Posts
        Yes , I looked through these docs. $xpdo->getCollection('..') works like a charm. However, the problem with $modx is that it is connected to default mysql database (psyab_modx), and my custom tables are in another database (psyab_data). So I need not only to set the custom prefix, but also to set up a custom connection. If I use $modx->request, it cannot handle that request as there are no such tables in modx default database. And $xpdo doesn't have a request method.
          • 11055 ☆ A M B ☆
          • 3,112 Posts
          you need to pass over the psyab's object by borrowing modx, eg, in the psyab class:
          public function connect () {
             return new xPDO('mysql:host=localhost;dbname=dbname', 'login', 'pass', 'prefix_');
          }
          


          then in your processor, you call it:
          $corePath = $modx->getOption('psyab.core_path',null,$modx->getOption('core_path').'components/psyab/');
           
          require_once $corePath.'model/psyAb/psyab.class.php';
           
          $modx->addPackage('psyAb',$corePath.'model/');
          $modx->psyab = new psyAb($modx);
          $modx->psyab->data = $modx->psyab->connect(); // just an example
           
          $modx->lexicon->load('psyab:default');
          //echo $modx->lexicon('desc');
          /* handle request */
          $path = $modx->getOption('processorsPath',$modx->psyab->config,$corePath.'processors/');
          $modx->request->handleRequest(array(
              'processors_path' => $path,
              'location' => '',
          ));
          
            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.
            • 41594
            • 4 Posts
            Thanks! Could you please give an example, how can I use this in handling requests? I.e. if I need to handle a getlist request to fetch the data from psyab_journal table, and this table is in modx database, I can simply add a getlist.class.php processor with this:
            class JournalGetListProcessor extends modObjectGetListProcessor {
                public $classKey = 'Journal';
                public $languageTopics = array('psyab:default');
                public $defaultSortField = 'title';
                public $defaultSortDirection = 'ASC';
                public $objectType = 'psyab.journal';
            }
            

            Class Journal is generated using parseSchema, and it is simply an empty class extending xPDOSimpleObject.

            Where should I call this custom $modx->psyab->connect() to get it working?
              • 11055 ☆ A M B ☆
              • 3,112 Posts
              Quote from: stop_psy at Oct 08, 2012, 04:22 AM

              Where should I call this custom $modx->psyab->connect() to get it working?

              Use my example as above, change your processor as below.

              <?php
              
              class JournalGetListProcessor extends modObjectGetListProcessor {
              
                  public $classKey = 'Journal';
                  public $languageTopics = array('psyab:default');
                  public $defaultSortField = 'title';
                  public $defaultSortDirection = 'ASC';
                  public $objectType = 'psyab.journal';
              
                  /**
                   * ///////////// You are overriding this method! ///////////////
                   *
                   * 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->psyab->data->newQuery($this->classKey);
                      $c = $this->prepareQueryBeforeCount($c);
                      $data['total'] = $this->modx->psyab->data->getCount($this->classKey, $c);
                      $c = $this->prepareQueryAfterCount($c);
              
                      $sortClassKey = $this->getSortClassKey();
                      $sortKey = $this->modx->psyab->data->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->psyab->data->getCollection($this->classKey, $c);
                      return $data;
                  }
              
              }
              
              return 'JournalGetListProcessor';
              


              Override other methods as well, if you need them and they try to connect to $this->modx's database. [ed. note: goldsky last edited this post 13 years, 11 months ago.]
                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.
                • 41594
                • 4 Posts
                Thank you! That's exactly what i was looking for.