New Community Forums are coming. Watch this space for news.
Subscribe: RSS
  • I'm continuing to struggle with xPDO connections to a foreign database in a snippet.

    This works fine as code inline:

    $dsn = 'mysql:host=localhost; dbname=database; port=3306; charset=utf8';
    $xpdo = new xPDO($dsn,'username','password');
    
    echo $o=($xpdo->connect()) ? 'Connected<br><br>' : 'Not Connected<br><br>';
    
    $results = $xpdo->query("SELECT * FROM `table` LIMIT 20");
    while ($row = $results->fetch(PDO::FETCH_ASSOC)) {
      $output .= $modx->getChunk('test', $row);
      }
    
    return $output;
    


    But when I put the connection script into a snippet 'pdoConnect'

    define('MODX_CORE_PATH', '/core/');
    define('MODX_CONFIG_KEY','config');
    require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
    
    $dsn = 'mysql:host=localhost; dbname=database; port=3306; charset=utf8';
    $xpdo = new xPDO($dsn,'username','password');
    


    and call it using

    $modx->runSnippet('pdoConnect');
    


    it throws an error


    Fatal error: Call to a member function connect() on a non-object in /home2/pghmin/public_html/core/cache/includes/elements/modsnippet/76.include.cache.php on line 22

    Modx 2.2.14 and 2.5.0 same problem.

    Seems trivial but I can't find a solution
    • If it's in a snippet, $modx and $xpdo are already set up for you, and are connected to the MODX database. You generally don't want to mess with them when connecting to a foreign DB.

      Since your code is using PDO, I would avoid the overhead of including the MODX class file and instantiating a second copy of xPDO. You should be able to just create a new PDO instance that connects to the foreign database and query it. $modx and $xpdo will still be available if you need them for something related to the MODX DB.

      http://php.net/manual/en/pdo.construct.php

      http://php.net/manual/en/pdo.query.php

        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
      • Thanks for your reply Bob. I fiddled with the code and ditched xPDO for a straight PDO connection.

        Once again it works fine if I have it all in the one snippet along with the query. But if I separate out the connection string and call it using $modx->runSnippet('pdoConnect'); I get the same error.

        I'm starting to think there might be something wrong with my usage of $modx->runSnippet. My expectation was that it's functionally equivalent to a require statement, but perhaps this is not the case?

        e.g. if I use require_once('assets/connections/test.php'); it works as expected so maybe that's the way to go. [ed. note: grogorio last edited this post 2 years, 4 months ago.]
        • I haven't looked lately, but my recollection is that runSnippet() just sets the properties included in the second argument (if any) and "includes" the code of the snippet. I could be wrong.

          I can't think of any reason why it would be different, unless you are using $modx or $xpdo as variables in your code.

          OTOH, using require_once would likely be faster than runSnippet() anyway.

          It would be faster yet (and would probably be more graceful in case of an error), to use "include" and check a return value from the snippet to see if the include succeeded.

          $returnVal = include 'MySnippetFile';

            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
          • I came back to this subject while working on another project and found a solution for my case, i.e. connecting to foreign databases.

            It was just a matter of storing the connection code in a regular php file (for example dbConnect.php) and then calling that in the snippet like so:

            require "assets/connections/dbConnect.php";


            Too easy
            • Better, you put this file at a more secure place, for example in your core/components/yourcomponent/ - directory, than under assets.
              Hopefully you've hardened your installation and have you core-directory outside of your webroot.
                -------------------------------

                you can buy me a beer, if you like MIGX

                http://webcmsolutions.de/migx.html

                Thanks!
              • I have a PHP file (that I cant make a snippet out of). I need to connect it to the database and rather then doing it the normal way was wondering if I could just include a header file of some sort that already has a DB connection.
                PlexKodiLucky Patcher
                • There's a reason that app is not available in the app store. Since one main purpose of LuckyPatcher is to steal apps, the authors are probably not among the most honest citizens on the web. I would want to see the source code of the program before letting it anywhere near my computer. It could be logging keystrokes, stealing your passwords, and sending them off to the authors.
                    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