We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38720
    • 23 Posts
    I want to get document variables during OnDocFormSave.
    Nothing seems to work for me.

    So, I want to get the 'pagetitle, longtitle, alias, content, etc...' after a document has been saved.
    I basically want to save these info into another table.

    Any pointers or example codes?
      • 4041
      • 788 Posts
      Maybe this will get you started:
      e = &$modx->Event;
      $out ='';
      
      switch ($e->name) {
      
          case 'OnDocFormSave':
      
              // $id = the document id which was just saved
      
              // get the document settings
              $doc_fields ='pagetitle,longtitle,alias,content';
              
              $doc = $modx->db->makeArray( $modx->db->select( $doc_fields, $modx->getFullTablename( 'site_content' ), 'id="'.$id.'"' ) );
      
      
              // check your custom table for an existing entry
              
              $where1 ='';
              
              $my_db_table = $modx->getFullTablename( 'MY_DB_TABLE' );
              
              $numrows = $modx->db->getValue( $modx->db->select( 'count(*)', $my_db_table, $where1 ) );
              
      
              // set up the insert / update fields
              $fields = array(
                      'field_name'     => $doc['field_value'],
                      'field_name2'    => $doc['field_value2']
                      );
                              
              if( $numrows > 0 ){
      
                  // we have an existing entry, so update
                  $where2 ='"MY_ID="'.$id.'"';
      
                  $update = $modx->db->update( $fields, $my_db_table, $where2 );
      
              }else{
      
                  // add a new entry
                  $insert = $modx->db->insert( $fields, $my_db_table );
      
              }
              
          break;
      
      }
      
      $e->output($out);
      return;
        xforum
        http://frsbuilders.net (under construction) forum for evolution
        • 38720
        • 23 Posts
        Sweet, just what I was looking for. Makes total sense. Thanks!
          • 4041
          • 788 Posts
          Glad it worked smiley

          Since you are just asking for one row of settings, you might change:
          $doc = $modx->db->makeArray( $modx->db->select( $doc_fields, $modx->getFullTablename( 'site_content' ), 'id="'.$id.'"' ) );


          to this:
          $doc = $modx->db->getRow( $modx->db->select( $doc_fields, $modx->getFullTablename( 'site_content' ), 'id="'.$id.'"' ) );


            xforum
            http://frsbuilders.net (under construction) forum for evolution
            • 38720
            • 23 Posts
            In the plugin, I also need to get the TVs for that document. Do you know how to get the value?

            I looked at the code below as an example, but it doesn't bring any results. Do I have to run a query to get what I need?

            $tvPr = $modx->getTemplateVarOutput(array("price"),$docid='2',$published=1);
            echo $tvPr['price'];
              • 38720
              • 23 Posts
              I ended up doing a query join to get the TVs. Thanks for your help.