We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 10449
    • 956 Posts
    What I’m trying to do is the following:

    I have to update a non-modx database table with values that are stored inside a modx-container.

    Every time a document inside container x is deleted, I need to trigger some kind of mySQL UPDATE / DELETE query.
    Every time a document inside container x is created, I need to trigger some kind of mySQL INSERT / UPDATE / DELETE query.

    Did someone achieve something similar already? Can you recommend something?

    I probably have to assign two separate system events for this, i.e. create two plugins.

    One would most probably be OnDocFormDelete. But is there something like OnDocFormCreate or OnDocFormNew also? I’m a bit stumped here... OnDocFormSave might work, but if I modify an existing document, I don’t need to trigger anything.
      • 1122
      • 209 Posts
      Your code -- be it plugin or something else -- should monitor the state of X container. Consider something like this:
      /* ------ one-time code */
      $_SESSION['Xs_children'] = <obtain list of all X's children>;
      
      /* ------ code repeated on some event (consider what) connected with backend */
      $Xs_children = <obtain list of all X's children>;
      // now, taking advantage of some PHP's array_diff* function, quickly find whether there is any difference
      // between $_SESSION['Xs_children'] and $Xs_children
      if (<$Xs_children is bigger than $_SESSION['Xs_children']>) {
        // user performed create
        <trigger SQL variant 1>;
        $_SESSION['Xs_children'] = $Xs_children;
      } elseif (<$Xs_children is smaller than $_SESSION['Xs_children']>) {
        // user performed delete
        <trigger SQL variant 2>;
        $_SESSION['Xs_children'] = $Xs_children;
      } else {
        // no action required
      }
      

      Hope this idea will help you.