We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 28120
    • 380 Posts
    I've got a TV multi select with related resources using the excellent guide http://rtfm.modx.com/display/revolution20/Creating+a+multi-select+box+for+related+pages+in+your+template#

    How do I simply maintain data integrity if a related resource is deleted. Looks like I'll have to go through all resources and manually edit the TV if they related to the non-existant page?

    Thanks

    Mark

      • 20031
      • 19 Posts
      I haven't read about the snippet, but I imagine you could make a plugin that triggers on:

      onResourceDelete
      onResourceUndelete
      onDocPublished
      onDocUnpublished

      That checks if the given resource exists as a relation in any other pages and updates them if necessary.

      OR

      you could add a check in the snippet that checks if the relation is valid.

      Makes sense?


        • 3749
        • 24,544 Posts
        I think OnResourceDelete would be the best choice, but it would be tricky since children of the resource are automatically deleted.

        In OnResourceDelete, the following variables are available:

        $id (ID of the resource)
        $resource (the resource object)
        $children (array of child IDs)
        

        OnResourceUndelete is also available, but the $children variable is not available (though the children are restored) and you wouldn't know what value to use for the TV.

        The cleanup code for onResourceDelete would look something like this (with some added sanity checks):

        <?php
        $tvId = 12; // Set to the ID of the TV
        $tvrs = $modx->getCollection('modTemplateVarResource', array('tmplvarid'=> $tvId));
        
        /* add the resource ID to the children array */
        $children[] = $id; 
        
        foreach ($tvrs as $tvr) {
           if (in_array($tvr->get('value'), $children)) {
               $tvr->remove();
           }
        }
        


        If it's a muli-select TV, things get more complicated. wink




        [ed. note: BobRay last edited this post 15 years, 1 month 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
          • 28120
          • 380 Posts
          Thanks both.

          Hey Bob, I've ordered your book!
            • 3749
            • 24,544 Posts
            Quote from: sparkyhd at Sep 04, 2011, 03:53 AM
            Thanks both.

            Hey Bob, I've ordered your book!

            Thanks!
              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