We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6795
    • 1 Posts
    How could I get page with all TV values? I use ModX API loaded externally from another PHP script.

    Problem is that I have to load about 300 pages like that simultaneously and each page contains about 30 TV’s - it’s take too much time.

    I use this code:

    
    function modx_get_page($id) {
     global $modx;
    
    
    
     $props = "archive_file_number,archive_password,col_article,col_brand,col_count,col_description,col_group,col_price,convert_encoding,csv_delimiter,last_exec,login_method,login_page,login_pass_code,login_user_code,pass,pop3_host,pop3_port,pop3_sender_domain,pop3_ssl,project_pass,project_url,project_user,result_action,schedule_period,schedule_repeat,skip_lines,supplier_id,type,url,user";
     $props = explode(',', $props);
    
     $resource = $modx->getObject('modResource', $id);
     $result = array(
      'id' => $resource->get('id'),
      'parent' => $resource->get('parent'),
      'pagetitle' => $resource->get('pagetitle'),
     );
    
     $properties = array();
    
     foreach($props AS $prop) $properties[$prop] = $resource->getTVValue($prop);
     $result['props'] = $properties;
    
     return $result; }
    


    Maybe there is some other way to do this?
      • 22448
      • 241 Posts
        • 28042 ☆ A M B ☆
        • 24,524 Posts
        My reaction to this is that you are going to need to re-think your design choices. In a case like this, a set of custom tables would be far more efficient. The MODx resource system is designed for optimum management of the generation of individual web pages, not for organizing and displaying such large collections of data. More than three or four TVs for a resource usually means a custom data management module should be considered.
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
          • 6795
          • 1 Posts
          I use Modx Revolution 2.1.1
            • 4172
            • 5,888 Posts
            As Susan suggested, I would store them in a custom-table. Than its easy to get them all with one db-query and getCollection.
              -------------------------------

              you can buy me a beer, if you like MIGX

              http://webcmsolutions.de/migx.html

              Thanks!
              • 6795
              • 1 Posts
              Maybe you right, but:
              Maybe there is some other way to do it based on Tvs? Maybe other API function?
                • 4172
                • 5,888 Posts
                well, there is no api-function for that, but its possible to create a complicated query, which gets all resources with all its TVs at once.

                [Edit]
                perhaps there is a way with getCollectionGraph
                  -------------------------------

                  you can buy me a beer, if you like MIGX

                  http://webcmsolutions.de/migx.html

                  Thanks!
                  • 6795
                  • 1 Posts
                  Quote from: Bruno17 at Jul 12, 2011, 04:46 AM

                  well, there is no api-function for that, but its possible to create a complicated query, which gets all resources with all its TVs at once.

                  [Edit]
                  perhaps there is a way with getCollectionGraph

                  Yes, it is!

                  But how? I think something like this
                  $query = $modx->query("SELECT `parent` FROM `modx_site_content` WHERE id = " . $current_id);
                  $parent = $query->fetchAll(PDO::FETCH_COLUMN);
                  $parent_id = $parent[0];

                  but i can’t imagine the right query...
                    • 3749
                    • 24,544 Posts
                    If you can use the raw value of the TVs, I think something like this would be the fastest (although I agree that a custom table would be a better option):

                    <?php
                    $tvts = $modx->getCollection('modTemplateVarResource', array('contentid'=>$modx->resource->get('id'));
                    
                    foreach ($tvts as $tvt) {
                    
                        $output .= 'TV ID: ' . $tvt->get('tmplvarid') . ' . . . VALUE: ' . $tvt->get('value');
                    }
                    return $output;


                    That should give you the ID of each TV and its raw value. You’d have to build the output you want from that.

                    If you want to explore doing it with a custom table, this might get you started: http://bobsguides.com/custom-db-tables.html

                    I’d build a table where the ID is the resource ID and each TV was a field. That way you wouldn’t have to mess with related objects and could just query that table directly for your values with something like this:

                    <?php
                    $tvs = $modx->getObject('modMyTvs',$modx->resource->get('id'));
                    
                    $output .= 'TV1 Caption: ' .  $tvs->get('Tv1FieldName') . "\n";
                    $output .= 'TV2 Caption: ' .  $tvs->get('Tv2FieldName') . "\n";
                    // etc.


                    Once the table was built, you could use the method in the tutorial to generate the classes and maps.
                      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