We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 8694
    • 61 Posts
    Yes. It works.
    Thank you so much for the help!

    smiley smiley smiley smiley
      • 3749
      • 24,544 Posts
      For the curious, after getting the TV’s value with

      $tv = $doc->getTVValue('docCount');

      or
      $tv = $doc->getTVValue(4); // 4 is the ID of the TV, not the doc


      You have the value, but you have no reference to the TV itself. Since setValue() is a member of the TV class, you need to get a reference to the TV first with getObject() before calling its setValue() member:

      $tv = $modx->getObject('modTemplateVar', array('name'=>'MyTv'));
      $tv->setValue($id,'SomeValue');  // $id is the ID of the doc where you want to change the TV , not the TV
        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
        • 17016
        • 138 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();
        



        Hi splittingred,
        thanks a lot. This works very well on my websites. Beside this I was trying to add a couple of different numbers in a TV called "row-of-numbers" step by step by using the php-command "array_push()" but it doesn´t work:

        $tvconv = $modx->getObject('modTemplateVar',array('name'=>'row-of-numbers'));
        
        $newValue = $modx->resource->getTVValue('row-of-numbers');
        array_push($newValue,"17");
        
        $tvconv->setValue($modx->resource->get('id'), $newValue);
        $tvconv->save();


        The result for the tv "row-of-numbers" should be like this 1 8 12 17

        Do you have any idea what I did wrong?

        Letti
          • 17016
          • 138 Posts
          Does anybody know what I have to do if I want to change a TV-Value of a resource other than the current document?

          I tried this but it seems like I did a mistake:

          $tvconv = $modx->getObject('modTemplateVar',array('name'=>'mytv'));
          $rawValue = $tvconv->getValue($idoftheotherresource);
          $newvalue = $rawValue + 3;
          $tvconv->setValue('mytv', $newValue);
          $tvconv->save();
            • 17016
            • 138 Posts
            Do I maybe have to add the id of the resource anywhere in the save-tag?
              • 3749
              • 24,544 Posts
              Try this:

              $tvconv->setValue($idoftheotherresource, $newValue);
                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
                • 17016
                • 138 Posts
                I just tried it like this:
                $tvconv = $modx->getObject('modTemplateVar',array('name'=>'mytv'));
                $rawValue = $tvconv->getValue($idoftheotheresource);
                $newvalue = $rawValue + 3;
                $tvconv->setValue($idoftheotheresource, $newValue);
                $tvconv->save();

                But it doesn´t work. For testing I changed the line:
                $tvconv->setValue($idoftheotheresource, $newValue);

                into:
                $tvconv->setValue($idoftheotheresource, '19');

                This successfully saves 19 into the TV "mytv" implying that this line is now still the problem:
                $newvalue = $rawValue + 3;


                Do you have any idea how to correct this line?

                Thanks a lot for your help!
                Letti
                  • 3749
                  • 24,544 Posts
                  Try this:
                  $newvalue = (int) $rawValue + (int) 3;
                    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
                    • 17016
                    • 138 Posts
                    Perfect!!! Now it works!!!

                    Do you think it is also possible to create step by step a growing row in a TV like it is possible in php by using the array_push()-command. For example like this:

                    array_push($newValue,"17");


                    On all purpose thanks a lot!!!

                    Letti
                      • 17016
                      • 138 Posts
                      The solution is much easier than I expected. It works by just using this:
                      $newvalue = $rawValue . 3;

                      Thanks again!!!
                      Letti