We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36926
    • 701 Posts
    The problem I have is random documents suddenly seem to have their data overwritten with another resources data. This seems to be very random as it only occasionally happens. From what i've seen it's only on resource that run a custom plugin so I'm assuming it's got to be this plugin. All the plugin does is upon publishing a doc it takes the current value for the content and the value of a TV, performs a str_replace.

    Can anyone see anything in the below thats likely to cause it to overwrite the resource with another resources values.

    <?php
    if ($resource->get('template') == 20) {
        $attachments = array('#attachment1','#attachment2','#attachment3','#attachment4','#attachment5','#attachment6');
    
        foreach($attachments as $k => $attachment) {
    
            $tvname = 'File'.++$k;
    
            if ($resource->getTVValue($tvname)) {
                $pattern[$k] = $attachment;
                $replace[$k] = $resource->getTVValue($tvname);
            }
    
        }
    
        unset($attachment);
    
        $id = $modx->resource->get('id');
        $pagetitle = $modx->resource->get('pagetitle');
        $tv = $modx->getObject('modTemplateVar', array('name'=>'additionalSummary'));
    
    //get content and summary text for resource 
        $content = $resource->getContent();
        $summary = $tv->getValue($id);
    
    //replace the link attachment strings   
        $newSummary = str_replace($pattern, $replace, $summary);
        $newContent = str_replace($pattern, $replace, $content);
    
    //set the new content and summary
        $resource->setContent($newContent);
        $tv->setValue($id,$newSummary);
        $tv->save();
        $resource->save();
    }
    


    Also I'd set up some logging so when this runs it outputs the current and new values for both content the TV value.





      • 3749
      • 24,544 Posts
      I would try this (if it makes sense):

              if (!empty($resource->getTVValue($tvname))) {
                  $pattern[$k] = $attachment;
                  $replace[$k] = $resource->getTVValue($tvname);
                  break;
              }


        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
        • 36926
        • 701 Posts
        Hey Bob,

        Thanks for the response. My email had that you'd asked if it was TV value or resource content, but can't see that now on the forum page now so guess you might of edited. But just to clarify, it's both the resource content and the TV value. But it's not always both at the same time.

        So some resources the content has been replaced with the content from another resource. Then on others the TV value has been replaced the value of another resources TV value. But this doesn't always happen and some are perfectly fine.

        The thing with the below, wont this only run once and then break out once of the foreach once it finds a value in the TV?
        Quote from: BobRay at Oct 16, 2014, 02:02 AM

                if (!empty($resource->getTVValue($tvname))) {
                    $pattern[$k] = $attachment;
                    $replace[$k] = $resource->getTVValue($tvname);
                    break;
                }



          • 3749
          • 24,544 Posts
          Yes, but it's only going to work for one of them anyway, since the operation you're performing on the resource is outside the loop.

            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
            • 36926
            • 701 Posts
            Quote from: BobRay at Oct 16, 2014, 06:22 PM
            Yes, but it's only going to work for one of them anyway, since the operation you're performing on the resource is outside the loop.


            Hi Bob,

            It was looping over them all and building array of values to replace and then performing that on the resource. This did seem to be working apart from it randomly deciding sometimes to use that values from another resource.

              • 3749
              • 24,544 Posts
              Sorry, yes I see that now. I think your problem may be that you're using both $resource and $modx->resource. You probably want $resource everywhere.
                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
                • 36926
                • 701 Posts
                Ok thanks Bob, i'll try that.