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

    Package, objectcontainers created by MIGX. I insert objects using formit and formit2db hook. The problem is that both createdon and createdby are not set automatically. I found that the update processor of migx sets those values. I could create those values in the formit2db snippet, but I rather like a more generic solution.

    I'm wondering if I could extend the generated class and set those values if not set already. The class which is created by MIGX looks like this:
    <?php
    class Name extends xPDOSimpleObject {}

    The xPDOSimpleObject has no access to the modX object (only the xpdo object).

    Do you have any ideas solving this?

    Thanks in advance!
    Huub

    This question has been answered by hvoort. See the first response.

      I love flexibility
      • 41874
      • 33 Posts
      You could change the formit2db snippet to update them like in here:
      https://forums.modx.com/thread/32560/formit2db-with-autocreate-schema-classes
        I love flexibility
        • 3749
        • 24,544 Posts
        Changing this code in the formt2db snippet:

        $allFormFields = $hook->getValues();    
        foreach ($allFormFields as $field=>$value){
             
            if ($field !== 'spam' && $field !== 'resource_id'){
                $dataobject->set($field,$value);
            }       
        }
         
        $dataobject->save();


        To this (untested):

        $allFormFields = $hook->getValues(); 
        unset($fields['spam'], $fields['resource_id']);
        
        $modx->runProcessor('resource/create', $allFormFields);
        


        might solve it. Back up the snippet first!


          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
        • discuss.answer
          • 41874
          • 33 Posts
          Actually the snippet has been changed a little bit, and looks like this:
          ...
          $formFields = $hook->getValues();
          foreach ($formFields as $field => $value) {
              if (!in_array($field, $removeFields)) {
                  if (in_array($field, $arrayFields)) {
                      switch ($arrayFormat) {
                          case 'json': {
                              $value = json_encode($value);
                              break;
                          }
                          case 'csv' :
                          default : {
                          $value = implode(',', $value);
                          break;
                          }
                      }
                  }
                  $dataobject->set($field, $value);
              }
          }
          ...


          But I get the idea. I actually solved it with a simple Formit hook:
          // Check if the submitted object already exists
                  if ($report instanceof Report) {
                      // Change UpdatedOn
                      $hook->setValue('editedon', date("Y-m-d H:i:s"));
                      $hook->setValue('editedby', $hook->modx->user->get("id"));
                  } else {
                      // Change CreatedOn
                      $hook->setValue('createdon', date("Y-m-d H:i:s"));
                      $hook->setValue('createdby', $hook->modx->user->get("id"));
                  }
                  return true;


          The check could be a simple id isset check, but due to some other performed checks before this code the object is already present (or not).

          Thanks a lot Bob!
            I love flexibility