We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36786 ☆ A M B ☆
    • 209 Posts
    I have little experience in xpdo, and by that I mean none. I was wondering if someone could tell me how to extend FormIt2db (https://forums.modx.com/thread/?thread=32560&page=1) to modify tables in a database other than the ModX database? I have it working flawlessly as is but I need the data stored separately from the modx database.

    From what I can tell it would involve something like this (bob's guides) but I have no idea how to proceed from there in modifying the snippet.
    $xpdo = new xPDO('mysql:host=localhost;dbname=mydatabase', 
    $database_user,$database_password,$table_prefix);
    


    Can anyone give me a leg up on this? Thanks!

    Any ideas?
      Ben Morrison
      Hacking templates to pieces since 2003
      • 4172
      • 5,888 Posts
      you just need to replace $modx with $xpdo:


      $dataobject=$xpdo->getObject($classname,array('id'=>$hook->getValue('resource_id')));
       
      if (empty($dataobject)){
          $dataobject = $xpdo->newObject($classname);
          //$dataobject->set('createdby', $modx->user->get('id'));
      }
      else{
          //$dataobject->set('editedby', $modx->user->get('id'));
      }


        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 36786 ☆ A M B ☆
        • 209 Posts
        Ok so it did't actually fix my problem, I get a an error "Could not create object Volunteers. I used the same schema created for a version of the snippet that worked on the local database. Could this be the issue, even though the tables are exactly the same? Here is my code for the snippet:

        <?php
        $prefix=$scriptProperties['prefix'];
        $packageName = $scriptProperties['packageName'];
        $tablename = $scriptProperties['tablename'];
        $database_user = $scriptProperties['databaseuser'];
        $database_password = $scriptProperties['databasepassword'];
        
        $xpdo = new xPDO('mysql:host=HOST;dbname=DATABASENAME', 
        $database_user,$database_password,$prefix);
         
        $packagepath = $xpdo->getOption('core_path') . 'components/'.$packageName.'/';
        $modelpath = $packagepath.'model/';
         
        $xpdo->addPackage($packageName,$modelpath,$prefix);
        $manager= $xpdo->getManager();
        $generator= $manager->getGenerator();
        $classname = $generator->getClassName($tablename);
         
        //$errorMsg = '<pre>'.print_r($scriptProperties['fields'],true).'</pre>';  
        //$hook->addError('error_message',$errorMsg);  
         
        //$scriptProperties['hook']->formit->config['emailTo']='[email protected]';
        $dataobject=$xpdo->getObject($classname,array('id'=>$hook->getValue('resource_id')));
         
        if (empty($dataobject)){
            $dataobject = $xpdo->newObject($classname);
            //$dataobject->set('createdby', $xpdo->user->get('id'));
        }
        else{
            //$dataobject->set('editedby', $xpdo->user->get('id'));
        }
        if (!is_object($dataobject) || !($dataobject instanceof xPDOObject)) {
            $errorMsg='Failed to create object of type: ' . $classname;
            $hook->addError('error_message',$errorMsg); 
            return false;
        }
         
        $allFormFields = $hook->getValues();    
        foreach ($allFormFields as $field=>$value){
             
            if ($field !== 'spam' && $field !== 'resource_id'){
                $dataobject->set($field,$value);
            }       
        }
         
        $dataobject->save();
         
        return true;
          Ben Morrison
          Hacking templates to pieces since 2003
          • 3749
          • 24,544 Posts
          Are you sure you wouldn't rather just move the tables into the MODX DB? That would speed things up and make your life a lot easier. wink



          ---------------------------------------------------------------------------------------------------------------
          PLEASE, PLEASE specify the version of MODX you are using . . . PLEASE!
          MODx info for everyone: http://bobsguides.com/modx.html
            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