We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 48586
    • 78 Posts
    Is it possible to write a form to an external database? I'm not really sure where to start with this one. I have prefix, tablename, packagename, and classname working. The issue is, we are modifying the site on a dev server, then when we push to our production server it adds a timestamp to our modx database.

    I have a coworker out of state who was able to pull a report of this form in csv on our old site by connecting to the external db to get the data, but now with the timestamp changing the modx db name with every push, it is no longer consistent for them.

    For example, it was much easier to write a script to pull a csv from database formList, rather than database modx1182736499 (with a timestamp that changes frequently)

    I apologize for the rambling, it has been a long night trying to figure this out
      • 4172
      • 5,888 Posts
      perhaps with some CURL - post?
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 48586
        • 78 Posts
        Modifying post, I was able to figure it out. Now I just need to get the order of the hooks set because my form is just refreshing the page on successful submit, and keeping the values in the fields [ed. note: rjohnson375 last edited this post 8 years, 10 months ago.]
          • 38309
          • 40 Posts
          it would be good if you could share how you got this to work.
          for the rest of us that are a bit slow. smiley
            • 48586
            • 78 Posts
            Quote from: bigben83 at Jun 22, 2015, 05:44 AM
            it would be good if you could share how you got this to work.
            for the rest of us that are a bit slow. smiley

            Hi, I apologize for the delay in my reply, I've been away at a conference. So there are a couple steps that I used to make this work, first, to connect to an external db to write my form data to I reused some code that I used for another part of my site that connects with an external db.

            First, I went to http://bobsguides.com/custom-db-tables.html, and used the CreateXpdoClasses snippet from the example. That created a folder in my core/components directory with a model and database schema, I used this as a placeholder for my real data.

            Then, I created another snippet with the following code called CreateXpdoExternalDBName:

            <?php
            define('MODX_CORE_PATH', '/var/www/whatever-your-path-is/core/');
            define('MODX_CONFIG_KEY','config');
            require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
             
            // Criteria for foreign Database
            $host = 'localhost';
            $username = 'mel';
            $password = '12345';
            $dbname = 'externalDBName';
            $port = 3306;
            $charset = 'utf-8';
              
            $dsn = "mysql:host=$host;dbname=$dbname;port=$port;charset=$charset";
            $xpdo = new xPDO($dsn, $username, $password);
             
            $manager= $xpdo->getManager();
            $generator= $manager->getGenerator();
              
            $xml= $generator->writeSchema('/var/www/whatever-your-path-is/core/components/externalDBName/schema/externalDBName.mysql.schema.xml','externalDBName', 'xPDOObject','TABLEPREFIX_');
             
            $xpdo->setLogLevel(xPDO::LOG_LEVEL_INFO);
            $xpdo->setLogTarget(XPDO_CLI_MODE ? 'ECHO' : 'HTML');
             
            $schema = '/var/www/whatever-your-path-is/core/components/externalDBName/schema/externalDBName.mysql.schema.xml';
            $target = '/var/www/whatever-your-path-is/core/components/externalDBName/model/';
            $generator->parseSchema($schema,$target);


            With the code done to connect to an external db, I then created my snippet that writes to the external db (called referFriend):

            <?php
            define('MODX_CORE_PATH', '/var/www/whatever-your-path-is/core/');
            define('MODX_CONFIG_KEY','config');
            require_once MODX_CORE_PATH . 'model/modx/modx.class.php';
            
            // Criteria for foreign Database
            $host = 'localhost';
            $username = 'mel';
            $password = '12345';
            $dbname = 'externalDBName';
            $port = 3306;
            $charset = 'utf-8';
             
            $dsn = "mysql:host=$host;dbname=$dbname;port=$port;charset=$charset";
            $dbconnection = new xPDO($dsn, $username, $password);
            
            $path = MODX_CORE_PATH . 'components/refer-a-friend/';
            
            $result = $modx->addPackage('refer-a-friend',$path .
                'model/','externalDBName');
            
            if (! $result) {
                return 'failed to add package';
            } else {
                
            $db_r_first_name = $hook->getValue('r_first_name');
            $db_r_last_name = $hook->getValue('r_last_name');
            $db_r_company_name = $hook->getValue('r_company_name');
            $db_r_account_number = $hook->getValue('r_account_number');
            $db_r_email_address = $hook->getValue('r_email_address');
            $db_r_home_phone = $hook->getValue('r_home_phone');
            $db_f_first_name = $hook->getValue('f_first_name');
            $db_f_last_name = $hook->getValue('f_last_name');
            $db_f_company_name = $hook->getValue('f_company_name');
            $db_f_address1 = $hook->getValue('f_address1');
            $db_f_address2 = $hook->getValue('f_address2');
            $db_f_city = $hook->getValue('f_city');
            $db_f_state = $hook->getValue('f_state');
            $db_f_zipcode = $hook->getValue('f_zipcode');
            $db_f_email_address = $hook->getValue('f_email_address');
            $db_f_home_phone = $hook->getValue('f_home_phone');
            $db_f_work_phone = $hook->getValue('f_work_phone');
            $db_f_work_ext = $hook->getValue('f_work_ext');
            
            
            $stmt = $dbconnection->query("INSERT into refer_a_friend (r_first_name,r_last_name,r_company_name,r_account_number,r_email_address,r_home_phone,f_first_name,f_last_name,f_company_name, f_address1,f_address2,f_city,f_state,f_zipcode,f_email_address,f_home_phone,f_work_phone,f_work_ext,timestamp ) 
            	VALUES 
            	('$db_r_first_name', '$db_r_last_name', '$db_r_company_name', '$db_r_account_number', '$db_r_email_address', '$db_r_home_phone', '$db_f_first_name', '$db_f_last_name', '$db_f_company_name', '$db_f_address1', '$db_f_address2', '$db_f_city', '$db_f_state', '$db_f_zipcode', '$db_f_email_address', '$db_f_home_phone', '$db_f_work_phone', '$db_f_work_ext', now())");
            	
            return true;
            }


            That's a general layout of how I wrote to the external DB, if you are copying the blocks of code above, just make sure the component names, db name, table name, etc are custom for you.

            Then, for my formit call it basically looks like:

            [[!FormIt?
            &hooks=`referFriend,email,redirect`
            .
            .
            .
            ]]


            I hope that helps [ed. note: rjohnson375 last edited this post 8 years, 9 months ago.]