Quote from: bigben83 at Jun 22, 2015, 05:44 AMit would be good if you could share how you got this to work.
for the rest of us that are a bit slow.
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.]