[[!CreateXpdoClasses? &myPackage=`users` &myPrefix=`users_`]]
[[!Register?
&submitVar=`registerbtn`
&activationResourceId=`9`
&activationEmailTpl=`ActivationEmailTpl`
&errTpl=`<span class="text-error">[[+error]]</span>`
&activationEmailSubject=`Thanks for Registering!`
&submittedResourceId=`8`
&usergroups=`Members`
&postHooks=`AddCustomUserData`
]]
<?php
$formFields = $hook->getValues();
$NewUser = $hook->getValue('register.user');
$id = $NewUser->get('id');
$fields = array(
'id' => $id,
'employeeID' => $formFields['empID'],
'department' => $formFields['dept'] );
$user = $modx->newObject('Custom', $fields);
$user->save();
any errors in the error-log?
what is the xpdo-schema of your Custom - table?
did you add your package somewhere with $modx->addPackage ?
Then, I wouldn't try to set the id-field programatically.
This should be an autoincrementing, primary-key-field, isn't it?
You should have another field for the relation between you custom-table and the user-table, for example something like 'user_id'
<?php
$path = MODX_CORE_PATH . 'components/users/';
$result = $modx->addPackage('users',$path . 'model/','users_');
if (! $result) {
return 'failed to add package';
} else {
$output = '<p>added Package</p>';
}
$users = $modx->getCollection('Custom');
$output .= '<p>Total: '. count($users) . '</p>';
foreach($users as $user) {
$fields = $user->toArray();
$output .= $modx->getChunk('tpl.ShowCustomDB', $fields);
}
return $output;
<?php
$formFields = $hook->getValues();
$NewUser = $hook->getValue('register.user');
$id = $NewUser->get('id');
$fields = array(
'userID' => $id,
'employeeID' => $formFields['empID'],
'department' => $formFields['dept'] );
$user = $modx->newObject('Custom', $fields);
$user->save();
<?xml version="1.0" encoding="UTF-8"?> <model package="users" baseClass="xPDOObject" platform="mysql" defaultEngine="MyISAM" version="1.1"> <object class="Custom" table="custom" extends="xPDOSimpleObject"> <field key="userID" dbtype="int" precision="11" phptype="integer" null="false" /> <field key="employeeID" dbtype="int" precision="10" attributes="unsigned" phptype="integer" null="false" /> <field key="department" dbtype="text" phptype="string" null="false" /> </object> </model>
<?php
$path = MODX_CORE_PATH . 'components/users/';
$result = $modx->addPackage('users',$path . 'model/','users_');
if (! $result) {
$modx->log(modX::LOG_LEVEL_ERROR,'Error message to the error log');
$hook->addError('error_message','Error message to the user');
return false;
}
$formFields = $hook->getValues();
$NewUser = $hook->getValue('register.user');
$id = $NewUser->get('id');
$fields = array(
'userID' => $id,
'employeeID' => $formFields['empID'],
'department' => $formFields['dept'] );
$user = $modx->newObject('Custom', $fields);
if (!(is_object($user) && ($user instanceof xPDOObject))) {
$modx->log(modX::LOG_LEVEL_ERROR,'Error message to the error log');
$hook->addError('error_message','Error message to the user');
return false;
}
if ($user->save() == false) {
$modx->log(modX::LOG_LEVEL_ERROR,'Error message to the error log');
$hook->addError('error_message','Error message to the user');
return false;
}
return true; // A successful hook should return true
You must load your package before you can use your custom classes in your post hook. Something like this should do the trick:
<?php $path = MODX_CORE_PATH . 'components/users/'; $result = $modx->addPackage('users',$path . 'model/','users_'); if (! $result) { $modx->log(modX::LOG_LEVEL_ERROR,'Error message to the error log'); $hook->addError('error_message','Error message to the user'); return false; } $formFields = $hook->getValues(); $NewUser = $hook->getValue('register.user'); $id = $NewUser->get('id'); $fields = array( 'userID' => $id, 'employeeID' => $formFields['empID'], 'department' => $formFields['dept'] ); $user = $modx->newObject('Custom', $fields); if (!(is_object($user) && ($user instanceof xPDOObject))) { $modx->log(modX::LOG_LEVEL_ERROR,'Error message to the error log'); $hook->addError('error_message','Error message to the user'); return false; } if ($user->save() == false) { $modx->log(modX::LOG_LEVEL_ERROR,'Error message to the error log'); $hook->addError('error_message','Error message to the user'); return false; } return true; // A successful hook should return true
[2012-11-01 07:52:25] (ERROR @ /ModX/connectors/security/user.php) Could not get table class for class: modAccess
[2012-11-01 07:52:25] (ERROR @ /ModX/connectors/security/user.php) Could not get table name for class: modAccess
[2012-11-01 07:52:25] (ERROR @ /ModX/connectors/security/user.php) Error 42000 executing statement:
Array
(
[0] => 42000
[1] => 1064
[2] => You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS `modAccess` WHERE `modAccess`.`principal` = 22' at line 1
)
[2012-11-01 07:52:25] (ERROR @ /ModX/connectors/security/user.php) Could not get table class for class: modAccess
[2012-11-01 07:52:25] (ERROR @ /ModX/connectors/security/user.php) Could not get table name for class: modAccess
[2012-11-01 07:52:25] (ERROR @ /ModX/connectors/security/user.php) Error 42000 executing statement:
Array
(
[0] => 42000
[1] => 1064
[2] => You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS `modAccess` WHERE `modAccess`.`principal` = 23' at line 1
)
[2012-11-01 07:53:11] (ERROR @ /ModX/index.php) Could not load class: Custom from mysql.custom.
[2012-11-01 07:53:11] (ERROR @ /ModX/index.php) Error message to the error log
[[!CreateXpdoClasses? &myPackage=`users` &myPrefix=`modx_users_` &myTables=`modx_users_Custom`]]