We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14883 ☆ A M B ☆
    • 450 Posts
    I am debugging some MODX code that was moved from one host to another and stopped working. The code takes an order number from the query string, looks it up in the database, and returns a record if one is found. The lookup stopped working properly after the migration.

    The code looked like this:

    • cacheable resource
    • resource contained cached chunk call
    • chunk contained uncached snippet call
    • snippet contained two include_once() calls and then a database connection, like this:

    include_once('config.php'); // declares array w/ config info (including database username & password).
    include_once('db.php'); // database connection functions
     $conn = db_connect($MY_CART["db_user"], $MY_CART["db_pass"]);
     $link = db_select($MY_CART["db_name"]);
    



    The include statements were succeeding, and the functions from the 2nd include were available, but the connect to the database but was failing. The values from the array in the first include_once were not available to the function call.

    On Friday I falsely diagnosed this as "I can't get variable declarations from included files, but functions are working fine." I rewrote config.php as a function that returned the array, then called that function, and everything worked:
    include_once('config.php'); // get_my_array() function
    include_once('db.php'); // database connection functions
    $MY_CART = get_my_array();
     $conn = db_connect($MY_CART["db_user"], $MY_CART["db_pass"]);
     $link = db_select($MY_CART["db_name"]);
    


    When called as a function like this, the $MY_CART array was available to the db functions and the code all worked. Hence my false diagnosis on Friday.

    This morning I dug a little deeper and found that the real issue is a matter of caching and "what's-available-when?" First I switched the first include back to its original form - just defining an array, not in a function. Then I added a var_dump of the config array.

    include_once('config.php'); // declares array w/ config info (including database username & password).
    include_once('db.php'); // database connection functions
    var_dump($MY_CART);
     $conn = db_connect($MY_CART["db_user"], $MY_CART["db_pass"]);
     $link = db_select($MY_CART["db_name"]);
    

    Output: NULL (and database error)

    Then I tried commenting out the database connection code:
    include_once('config.php'); // declares array w/ config info (including database username & password).
    include_once('db.php'); // database connection functions
    var_dump($MY_CART);
     //$conn = db_connect($MY_CART["db_user"], $MY_CART["db_pass"]);
     //$link = db_select($MY_CART["db_name"]);
    

    Output: full vardump of MY_CART array

    Hmm. So this debunks my weird conspiracy theory about not being able to access variables from include files.

    As long as the database code doesn't ask for it, the array is available.

    That started leading me to think about caching.

    I decided to try calling the original snippet cached. All the code worked as intended - the db user info from the array was available to the functions.

    However, in this setup, the lookup of the order number from the query string also gets cached.

    The final working solution was to call the snippet as cached, but change the resource itself to "non-cacheable".

    Everything works.

    I'm just hoping someone can now explain it to me. Why, if the snippet is called uncached, are the variables declared in the first include_once call not available to the functions from the second include_once call?
      • 18373 ☆ A M B ☆
      • 3,141 Posts
      I'm guessing you'll need to be more specific in defining where your files are, because your snippet gets turned into a php function located in core/cache/includes/modsnippet when it hits the source cache. Maybe something as simple as MODX_BASE_PATH.'/config.php' would have sorted your issue too.
        Mark Hamstra • Developer spending his days working on Premium Extras and a MODX Site Dashboard with the ability to remotely upgrade MODX and extras to make the MODX world a little better.

        Tweet me @mark_hamstra, check my infrequent blog at markhamstra.com, my slightly more frequent ramblings at MODX.today or see code at Github.
        • 14883 ☆ A M B ☆
        • 450 Posts
        Quote from: markh at Mar 18, 2013, 11:33 AM
        I'm guessing you'll need to be more specific in defining where your files are, because your snippet gets turned into a php function located in core/cache/includes/modsnippet when it hits the source cache. Maybe something as simple as MODX_BASE_PATH.'/config.php' would have sorted your issue too.

        Mark,

        In the actual code, the full path to the include files is specified. I don't think path issues factor in to this at all. While troubleshooting I switched these to require_once (and also tried include and require without the 'once') and never got an error unless I purposely tried to change the filename to a non-existent file.

        Is there anything else about paths for include files that would affect the processing order and/or availability of the included variables?
          • 3749
          • 24,544 Posts
          Is the code that includes the file below the code that uses the variables?

          Is the code that uses the variables inside a function, class, or method?

          Are there properties in the snippet tag that might not be available at the time it's parsed?


          Those are the only conditions I can think of that would cause what you're seeing.
            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
            • 14883 ☆ A M B ☆
            • 450 Posts
            Quote from: BobRay at Mar 18, 2013, 12:00 PM
            Is the code that includes the file below the code that uses the variables?

            Is the code that uses the variables inside a function, class, or method?

            Are there properties in the snippet tag that might not be available at the time it's parsed?


            Those are the only conditions I can think of that would cause what you're seeing.

            The include directives are the first lines the snippet.

            The variables are used as arguments in a function call, as shown in the example.

            There are no properties being passed to the snippet.

            As soon as I changed from cached resource / uncached snippet to uncached resource / cached snippet, the code started working as written.

              • 14883 ☆ A M B ☆
              • 450 Posts
              This is on 2.2.0-pl2.

              I haven't tried to recreate it anywhere else.
                • 3749
                • 24,544 Posts
                Are you initializing those two variables or the $MY_CART array in db.php? That could be wiping them out.
                  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
                  • 14883 ☆ A M B ☆
                  • 450 Posts
                  Quote from: BobRay at Mar 18, 2013, 12:27 PM
                  Are you initializing those two variables or the $MY_CART array in db.php? That could be wiping them out.

                  Funny you should mention that. I did trip myself up with exactly that while troubleshooting - redeclared the array in the "functions.php" file and forgot I had done so. But no, it isn't doing that at all now.
                    • 3749
                    • 24,544 Posts
                    It's weird all right. wink
                      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
                      • 37638
                      • 63 Posts
                      Does $MY_CART get uninitialized in any of your code, even past the point your are var_dump'ing it?

                      Is the snippet being called more than once on the resource? I.e., is either the snippet being iterated over in the chunk, or the chunk being iterated over the template?