We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 32678
    • 290 Posts
    I'm in the process of upgrading a site from Evo to Revo and followed the instructions here (http://wiki.modxcms.com/index.php/Create_TV-Based_Chunks) to rewrite snippets that allow the user to select chunks within certain templates.

    I get an error when trying to apply the relevant template to new resources:

    Fatal error: Call to a member function getMany() on a non-object in /home/wesleypi/public_html/revo/core/cache/includes/elements/modsnippet/6.include.cache.php on line 11

    This references the getChunk snippet that retrieves the names of the chunks for selection via the associated TV.

    Here are lines 10/11:

    /* Get the chunk objects in that category */
    $chunkArray = $catObj->getMany('Chunks');

    Can anyone tell me why this is causing the error?

    I'm using Revo 2.2.8.

    Thanks!
      • 3749
      • 24,544 Posts
      The code that's creating $catObj is not working. We'd need to see it to help you (and please put it in code tags using the <> button on the toolbar).


        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
        • 32678
        • 290 Posts
        Sure - thank you for taking a look. Both snippets and the eval statement in the tv are below.

        As I grabbed this code, I noticed that the closing php bracket is not present, though this was included when the snippets were created. I opened up a couple snippets that I brought in via Package Manage and they, too, lack the closing bracket, so I assume this is normal.

        Here's getChunks:

        <?php
        /* Get the category object */
        $catObj = $modx->getObject('modCategory',array('category'=>$category));
         
        /* Get the chunk objects in that category */ 
        $chunkArray = $catObj->getMany('Chunks'); 
         
        $chunkNames=array();
         
        /* Put the chunk names into the array */ 
        foreach($chunkArray as $chunk) {
            $chunkNames[] = $chunk->get('name');
        }
         
        /* Format the chunk names as a delimited string for the TV */ 
        $l_chunks = implode("||",$chunkNames); 
        return $l_chunks;


        Here's chunkListParser:

        <?php
        $ar_chunks = explode(',',$chunks);
        $ar_chunks = array_unique($ar_chunks);
         
        $output = '';
         
        foreach ($ar_chunks as $chunk) {
          if ($chunk != '') {
            $c = $modx->getChunk($chunk);
            if ($c) $output .= $c;
          }
        } 
         
        if ($output != '' && isset($wrapperdiv)) {
          $output = '<div id="'.$wrapperdiv.'">'.$output.'</div>';
        }
         
        return $output;


        Here's the TV eval statement:
        @EVAL return $modx->runSnippet('getChunks',array('category'=>'Image Page - Price Sets'));


        Thanks again.
          • 3749
          • 24,544 Posts
          Yes, the missing closing PHP tag is normal.

          I don't see anything wrong with your code. You could try renaming your category to something without spaces or hyphens.

          Or, as an alternative, you could use the ID of the category, which might be a little faster anyway:


          $chunkArray = $modx->getCollection('modChunk', array('category' => $categoryId));
          [ed. note: BobRay last edited this post 13 years, 2 months ago.]
            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
            • 32678
            • 290 Posts
            Thanks. I edited the category name to remove spaces and hyphens and that did the trick.

            Just so I'm clear, the line of code you provided in your reply to use category ID replaces the following, correct?
            $catObj = $modx->getObject('modCategory',array('category'=>$category));

            Thanks,
            Wesley
              • 3749
              • 24,544 Posts
              It actually replaces both these lines:

              $catObj = $modx->getObject('modCategory',array('category'=>$category));
              $chunkArray = $catObj->getMany('Chunks'); 



              In case you're curious, the original code does a database query to get the category object from its table, then a call to the category's getMany() function, which parses the alias ('Chunks') and then makes another query to get the associated objects from a different table (though in either case, the objects might be retrieved from the cache).

              The code I supplied just makes one query to the DB asking for chunks with that category ID in their 'category' field.
                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