if($modx->getOption('use_foreign_database')) { // $database_dsn, $foreign_database_user, $foreign_database_password variables are set here $db = new xPDO($database_dsn, $foreign_database_user, $foreign_database_password); // Test connection $connected = ($xpdo->connect()) ? 'Connected' : 'Not Connected'; $modx->log(xPDO::LOG_LEVEL_DEBUG, $connected); } else { $db = $modx; } $package_path = MODX_CORE_PATH . 'components/dancers/model/'; $db->addPackage('dancers',$package_path,'ods_'); $query = $db->newQuery('Payroll'); $query->innerJoin('PayrollPayPeriod','PayrollPayPeriod',array("Payroll.pay_period_id = PayrollPayPeriod.id")); $query->groupBy('employee_id'); $query->select(array('Payroll.id, employee_id, sum(gross_income) as gross_income, PayrollPayPeriod.id, PayrollPayPeriod.pay_date')); $query->where(array( 'PayrollPayPeriod.pay_date:>=' => date($year . '-' . $month . '-01'), 'PayrollPayPeriod.pay_date:<=' => date($year . '-' . $month . '-t') )); $payrolls = $db->getCollection('Payroll', $query); foreach ($payrolls as $payroll) { $modx->log(xPDO::LOG_LEVEL_DEBUG, $payroll->get('employee_id')); $modx->log(xPDO::LOG_LEVEL_DEBUG, $payroll->get('PayrollPayPeriod.pay_date')); $modx->log(xPDO::LOG_LEVEL_DEBUG, $payroll->get('gross_income')); }
This question has been answered by nick2687. See the first response.
$db = new xPDO($database_dsn, $foreign_database_user, $foreign_database_password, array ( XPDO::OPT_HYDRATE_FIELDS => true, XPDO::OPT_HYDRATE_RELATED_OBJECTS => true, ));
Found some help in the slack community I was not hydrating the fields when connecting to the remote database, adding the below array fixed my issue.
$db = new xPDO($database_dsn, $foreign_database_user, $foreign_database_password, array ( XPDO::OPT_HYDRATE_FIELDS => true, XPDO::OPT_HYDRATE_RELATED_OBJECTS => true, ));