We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36517
    • 14 Posts
    Working on migrating a site from evolution to revolution. Have used modX before but it;s been a while, and I didn't run into this problem last time. I can't use
    $modx->getCollection('modResource',$c); 
    in my code - I get a PHP error:

    Fatal error: Call to a member function getCollection() on a non-object in [path-to-root]/core/cache/includes/elements/modsnippet/17.include.cache.php on line 21


    Here is the snippet:

    <?php
    global $modx;
    $modx->resource =& $document;
    $id = $modx->resourceIdentifier;
    $arr = array("parent" => $id, "published" => 1, "deleted" => 0);
    $c = $modx->newQuery('modResource');
    $c->where(array(
       'parent:IN' => array(23,24,25),
       'deleted' => false,
       'hidemenu' => false,
       'published' => true,
    ));
    $c->sortby('menuindex','ASC');
    $c->limit(10);
    $resources = $modx->getCollection('modResource',$c);
    $children= $modX->getCollection('modResource', $arr);
    if (!$children === false) {
        $firstChild= $children[0];
        $firstChildUrl= $modx->makeUrl($firstChild['id']);
    } else {
        $firstChildUrl= $modx->makeUrl($modx->config['site_start']);
    }
    return $modx->sendRedirect($firstChildUrl);
    


    The lines don't match up obviously, and I'm trying to debug it so that's why the code is not actually reasonable and has two parts that would do the same thing, but I'm wondering why modResource is giving me the non-object error? II can't find any instructions on initializing it (sorry if my terminology is off) for snippets but I assume that it's not defined.

    Thanks for any help you can provide. I am using PHP v5, modx version 2.1.3-pl (traditional)
      • 3749
      • 24,544 Posts
      This line is causing your error:

      $children= $modX->getCollection('modResource', $arr);
      
      
      It should be $modx.

      Lines 2 and 3 should be removed. Line 2 is unnecessary and line 3 trashes the $modx-resource object (probably not what you want to do). 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
        • 36517
        • 14 Posts
        Thank you! That was very helpful. Found some other problems in the old way the snippet was written - now the array returned by $children has keys, so $children[0] is empty, but that's fine! Thanks for the help!

        Oh, I realised why I used $modX instead of $modx - it's $modX here: http://rtfm.modx.com/display/revolution20/Summary+of+Legacy+Code+Removed+in+2.1 [ed. note: kblair last edited this post 15 years, 1 month ago.]