We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 21414
    • 41 Posts
    I'm working on a hook for formit to loop over rows of fields and create multiple resources at once.

    Currently while trying to figure this out I have a form containing the following table:

     <table class="table responsive-table">
                <thead>
            
                    <th>pagetitle</th>
                    <th>longtitle</th>
                </thead>
                <tbody>
                    <tr>
                    
                    <td><input type="text" name="pagetitle[]" value="" /></td>
                    <td><input type="text" name="longtitle[]" value=""/></td>    
                    </tr>
                    <tr>
                    <td><input type="text" name="pagetitle[]" value="" /></td>
                    <td><input type="text" name="longtitle[]" value=""/></td>    
                    </tr>
                </tbody>
                
            </table>
    


    From that I want to loop over each row to create multiple resources with the provided pagetitles & other values.

    Currently my snippet looks like this:
    <?php
    $allFormFields = $hook->getValues(); 
    
    foreach ($allFormFields as $row)
    {
        
        $doc = $modx->newObject('modResource');
        $doc->set('createdby', $modx->user->get('id'));
        
            foreach ($row as $field=>$value) {
                
                $doc->set($field, $value);
             
            }
    $doc->save();
     
    }
    return true;
    
    


    This is working in that it is creating 2 resources as it should however it is always setting my field values to blank. The log is showing that im trying to call set() on the field name "Array". I think i'm missing something obvious here to return the array key & value instead of the string "array()"

    This is within my debug log:
    [2014-06-10 19:46:54] (ERROR @ /modx/index.php) xPDOObject - Called set() with an invalid field name: Array
    (
        [0] => 0
    )
    
    [2014-06-10 19:46:54] (ERROR @ /modx/index.php) xPDOObject - Called set() with an invalid field name: Array
    (
        [0] => 0
        [1] => 1
    )
    
    [2014-06-10 19:46:54] (ERROR @ /modx/index.php) xPDOObject - Called set() with an invalid field name: Array
    (
        [0] => 0
        [1] => 1
        [2] => 0
    )
    

      • 21414
      • 41 Posts
      I've also tried using the following:

      <?php
      $allFormFields = $hook->getValues(); 
      
      
      foreach ($allFormFields as $key => $value)
      {
          $doc = $modx->newObject('modResource');
          $doc->set('createdby', $modx->user->get('id'));
          $doc->set('pagetitle', $value['pagetitle']);
          $doc->set('longtitle', $value['longtitle']);
          $doc->save();
      }
      
      
      
       
      return true;


      but that seems to set both my fields to be "Array"...
        • 3749
        • 24,544 Posts
        You need to see what $hook->getValues() is returning.

        See if this gets you anything:

        $allFormFields = $hook->getValues(); 
        echo '<pre>' . print_r($allFormFields, true);
        die();


        If not, create a chunk called debug and do this:

        $chunk = $modx->getObject('modChunk', array('name' => 'debug'));
        $chunk->setContent(print_r($allFormFields, true));
        $chunk->save();


        Then, see what's in the chunk.


          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
          • 4172
          • 5,888 Posts
          I think, you will need something like that (untested):

          <?php
          
          $allFormFields = $hook->getValues();
          
          $resources = array();
          
          foreach ($allFormFields as $field => $values) {
              if (is_array($values)) {
                  foreach ($values as $key => $value) {
                      $resources[$key][$field] = $value;
                  }
              }
          }
          
          foreach ($resources as $resource) {
              $doc = $modx->newObject('modResource');
              $doc->fromArray($resource);
              $doc->set('createdby', $modx->user->get('id'));
              $doc->save();
          }
          return true;
          
          [ed. note: Bruno17 last edited this post 9 years, 11 months ago.]
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 21414
            • 41 Posts
            Thanks Bruno that worked perfectly. I was trying to wrap my head around how the array would look but wasn't quite getting there.

            Also, this worked great bob for actually seeing what was returned, going to keep this saved for later...

            $chunk = $modx->getObject('modChunk', array('name' => 'debug'));
            $chunk->setContent(print_r($allFormFields, true));
            $chunk->save();
            


            Thank you both.