We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 44234
    • 219 Posts
    Hi,

    Have a very strange problem that has me completly stumped.

    I have two custom snippets that query a custom database. They both work when ONLY ONE is called on a page, however when I place both calls on the same page the first works but the second one fails.

    I have tried debugging using '$xpdo->setDebug(true);' and the second failing snippet returns:
    Could not get table class for class: Eng1

    Any ideas??

    This question has been answered by multiple community members. See the first response.

      Find me on Twitter, GitHub or Google+
    • Can you share some of the code from these snippets? Especially anything that could be common between them?
        • 44234
        • 219 Posts
        Yeah the first snippet starts like so:
        $xpdo = new xPDO('mysql:host=localhost;dbname=leagues',$username,$password);
        //$xpdo->setDebug(true);
        
        /* set default properties */
        $leagueName = !empty($leagueName) ? $leagueName : '';
        $rowTpl = !empty($rowTpl) ? $rowTpl : '';
        $output = '';
        
        if (!$xpdo->addPackage('league_pack','C:/xampp/htdocs/repo/revolution/core/components/league_pack/model/','')) {
          return 'Can not load package';
        }
        
        $c = $xpdo->newQuery($leagueName);
        $c->select('id,home');
        $c->sortby('home','ASC');
        $rows = $xpdo->getCollection($leagueName,$c);
        
        foreach ($rows as $row) {
          $line = $row->toArray('',false,true);
          $data[] = $line[home];
        }
        
        $rows = array_values(array_unique($data));
        
        foreach ($rows as $row) {
          $ph[] = $modx->setPlaceholder('teamName', $row);
          $result .= $modx->getChunk($rowTpl,$ph);
        }
        
        etc...
        

        and the second:
        $xpdo = new xPDO('mysql:host=localhost;dbname=leagues',$username,$password);
        //$xpdo->setDebug(true);
        
        /* set default properties */
        $league = !empty($league) ? $league : '';
        $htscore = $htscore != 'na' ? explode('-', $htscore) : 'na';
        $season = !empty($season) ? $season : '';
        $goaltime = $goaltime != 'na' ? explode('-', $goaltime) : 'na';
        $gtFilterVar01 = $goaltime != 'na' ? $goaltime[1] : false;
        $gtFilterVar02 = $goaltime != 'na' ? $goaltime[2] : false;
        $eqlGoaltime = $eqlGoaltime != 'na' ? explode('-', $eqlGoaltime) : 'na';
        
        if (!$xpdo->addPackage('league_pack','C:/xampp/htdocs/repo/revolution/core/components/league_pack/model/','')) {
            return 'Can not load package';
        }
        
        //Last updated
        $d = $xpdo->newQuery($league);
        $d->sortby('id','DESC');
        $d->limit(1);
        $d = $xpdo->getObject($league,$d);
        $updated = !empty($d) ? $d->get('date') : '';
        
        $c = $xpdo->newQuery($league);
        
        if ($htscore != 'na') {
          $c->where(array(
            'hthg' => $htscore[0],
            'htag' => $htscore[1],
          ));
        }
        if ($season != 'na') {
          $c->where(array(
            'season' => $season,
          ));
        }
        
        $rows = $xpdo->getCollection($league,$c);
        
        foreach ($rows as $row) {
          $array = $row->toArray();
        
        etc...
        

        Rough and ready at the moment but like I said, both work correctly with no errors when called on their own.

        Thanks
          Find me on Twitter, GitHub or Google+
        • discuss.answer
          Have you considered sharing a single $xpdo connection to this custom database? You can have one snippet—which executes first—create the database connection and set the package, then set the xPDO instance as placeholder. Then these two snippets can use that placeholder to get the single xPDO instance to work with.

          Not sure without debugging what could be the issue with the second snippet not working if the other runs, but I imagine it has something to do with trying to reload the same base classes multiple times in a single request...
            • 3749
            • 24,544 Posts
            You might also consider moving the tables into the MODX database so you could use the connection that's always available through the $modx object.
              Did I help you? Buy me a beer
              Get my Book: MODX:The Official Guide
              MODX info for everyone: http://bobsguides.com/modx.html
              My MODX Extras
              Bob's Guides is now hosted at A2 MODX Hosting
              • 44234
              • 219 Posts
              Quote from: opengeek at Nov 25, 2014, 06:49 PM
              Have you considered sharing a single $xpdo connection to this custom database? You can have one snippet—which executes first—create the database connection and set the package, then set the xPDO instance as placeholder. Then these two snippets can use that placeholder to get the single xPDO instance to work with.

              I'm sure this is the issue, how would I go about setting the placeholder and use it in other snippet?

              Thanks Jason
                Find me on Twitter, GitHub or Google+
              • discuss.answer
                • 41874
                • 33 Posts
                Use the $modx object with getPlaceholder and setPlaceholder. See documentation for more details.

                http://rtfm.modx.com/revolution/2.x/developing-in-modx/other-development-resources/class-reference/modx/modx.setplaceholders

                An example could be:

                Make sure you have the $modx object. See section "Code for CreateXpdoClasses" http://bobsguides.com/custom-db-tables.html
                [Edit: In a snippet you already have access to the $modx object.]

                // Get $modx
                // Get xpdo
                $xpdo = $modx->getPlaceholder("my.xpdo"); 
                if ($xpdo == "") { 
                  $xpdo = new xPDO('mysql:host=localhost;dbname=leagues',$username,$password);
                  $modx->setPlaceholder("my.xpdo", $xpdo);
                }
                // Use xpdo


                Please test the if statement properly. The Api states thet the result is an empty string it it does not exists: http://api.modx.com/revolution/2.2/db_core_model_modx_modx.class.html#%5CmodX::getPlaceholder()
                  I love flexibility
                  • 44234
                  • 219 Posts
                  Thanks for the example hvoort, I'll give this a go tomorrow
                    Find me on Twitter, GitHub or Google+
                    • 44234
                    • 219 Posts
                    Thanks for the help, this is now fixed. The issue was trying to set a database connection twice on the same page.

                    hvoort's code works straight out the box! I used it in a third snippet which is called first to set the connection. Then reuse the connection placeholder in my other snippets.

                    Thanks again!
                      Find me on Twitter, GitHub or Google+