We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 8694
    • 61 Posts
    Hello,

    I write on a snippet for revolution.
    I can read the value of a TV from an specific dokument with this function:

    $doc->getTVValue('docCount');


    But what I need now is the opposite: I want write the value of the TV.

    Have anyone a idea?
      • 1778
      • 659 Posts
      Hello

      Try this :

      /*set the TV value
      $tv->setValue($tv, 'new value');
      


      or maybe

      /*set the TV value
      $tv->setTVValue($tv, 'new value');
      


      Don’t forget to save() after reset your TV...
      Cheers
        • 8694
        • 61 Posts
        Hi Anso,

        thank you for your reply.
        I try this:

         $tv->setTVValue('docCount', 'new value');

        (docCount ist the name of the TV)

        I get an error like this:
        Fatal error: Call to a member function setTVValue() on a non-object in www...
          • 1778
          • 659 Posts
          If you have :
          $doc->getTVValue('docCount');
          


          You can try this it should work I think
          //get the value of the TV
          $tv = $doc->getTVValue('docCount');
          
          // set the value
          $tv->setTVValue('docCount', 'new value');
          


          $tv "stores" the TV value, then you can act on it...

          I suppose it’s possible like that too :
          setTVValue('$doc->getTVValue('docCount')','new value');
          


          Hope this helps
          Cheers
            • 8694
            • 61 Posts
            Hi,

            both did not Work. I get an PHP error: "Call to a member function..."

            Here is my complete Code:
            // Get ID of current document:
            $docID = $modx->resource->get('id');   
            
            // Get title of current document:
            $docTitle = $modx->resource->get('pagetitle');
            
            // Get the value of the TV docCount:
            $doc = $modx->getObject('modResource', $docID);
            //echo $doc->getTVValue('docCount');
            
            // Add 1 to the value of docCount and call it newValue:
            $newValue = $doc->getTVValue('docCount') + 1;
            

            To that point the code ist working.
            $newValue is the value that I want to write into the TV docCount now.
              • 1778
              • 659 Posts
              I didn’t try this but I think it should work... Let me know

              // get ID of the current document
              $docID = $modx->resource->get('id');
              
              // get the TV docCount in the current doc 
              $tv = $modx->getObject('modTemplateVar',array('name'=>'docCount'));
              
              // Add 1 to the value of docCount and call it newValue:
              $newValue = $doc->getTVValue('docCount') + 1;
              
              // Set the tv docCount with new value
              $tv->setValue($docID, $newValue);
              $tv->save();
              


              Hope this helps...
              Cheers
                • 8694
                • 61 Posts
                Hi Anso,

                I try your example at I get an PHP error:
                Fatal error: Call to a member function setValue() on a non-object in www....core/cache/elements/modsnippet/14.include.cache.php on line 20
                  • 28215
                  • 4,149 Posts
                  That’s because it should be this:

                  // get the TV docCount in the current doc 
                  $tv = $modx->getObject('modTemplateVar',array('name'=>'docCount'));
                  
                  // Add 1 to the value of docCount and call it newValue:
                  $newValue = $modx->resource->getTVValue('docCount') + 1;
                  
                  // Set the tv docCount with new value
                  $tv->setValue($modx->resource->get('id'), $newValue);
                  $tv->save();
                  

                    shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
                    • 1778
                    • 659 Posts
                    I tried this one and it works fine

                    <?php
                    // get ID of the current document
                    $docID = $modx->resource->get('id');
                      $doc=$modx->resource;
                    
                    // get the TV Color in the current doc
                    $tv = $modx->getObject('modTemplateVar',array('name'=>'Color'));
                    
                    // Add 1 to the value of docCount and call it newValue:
                    $newValue = "red";
                    
                    // Set the tv Color with new value
                    $tv->setValue($docID, $newValue);
                    $tv->save();
                      $doc->save();
                    ?>
                    


                      • 1778
                      • 659 Posts
                      Quote from: splittingred at Aug 11, 2010, 08:24 AM

                      That’s because it should be this:

                      // get the TV docCount in the current doc 
                      $tv = $modx->getObject('modTemplateVar',array('name'=>'docCount'));
                      
                      // Add 1 to the value of docCount and call it newValue:
                      $newValue = $modx->resource->getTVValue('docCount') + 1;
                      
                      // Set the tv docCount with new value
                      $tv->setValue($modx->resource->get('id'), $newValue);
                      $tv->save();
                      



                      Yep I’d forgot to declare "$doc=$modx->resource;" so my "$doc->getTVValue" cannot work of course !sorry for this...