We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 41364
    • 9 Posts
    I am trying to learn xPDO and need some help. I have went through bob's guide http://bobsguides.com/custom-db-tables.html and I was able to follow along and get everything in the guide to work. I felt like I understood it and it was a well written guide.

    What I am trying to do is: create a snippet to use as a postHook for the Login.Register snippet that will take the extra fields i have added and insert them to my users_custom table in the database. I know I could just use them as extended fields but I need to be able use them for sorting, calculating and other things and I also just want to learn xPDO while I am at it.

    Using:
    MODX Revolution 2.2.4-pl (traditional)
    Login 1.8.1

    Created my classes with this.
    [[!CreateXpdoClasses? &myPackage=`users` &myPrefix=`users_`]]


    Register snippet call
    [[!Register?
        &submitVar=`registerbtn`
        &activationResourceId=`9`
        &activationEmailTpl=`ActivationEmailTpl`
        &errTpl=`<span class="text-error">[[+error]]</span>`
        &activationEmailSubject=`Thanks for Registering!`
        &submittedResourceId=`8`
        &usergroups=`Members`
        &postHooks=`AddCustomUserData`    
    ]]
    


    Snippet: 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();
    


    The register form posts and the user is added to the modx_users tables but then it hangs on the postHook and doesn't redirect to the submittedResource.

    Any help is greatly appreciated. [ed. note: adam87 last edited this post 13 years, 11 months ago.]
      • 4172
      • 5,888 Posts
      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'
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 41364
        • 9 Posts
        Quote from: Bruno17 at Oct 31, 2012, 01:59 PM
        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'


        Code that used addpackage from bob's Guide.
        <?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;


        Good catch with id field, it is the primary key. I updated it to autoincrementing and add a userID field for the relation between the tables. New snippet as follows:

        <?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();
        


        I looked at my Schema and saw that it had 2 Id objects due to my goof with not having the id field primary and as autoincrement.

        So I removed the package and ran CreateXpdoClasses. Here is the current Schema

        Schema:
        <?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>
        


        Still does not work.

        I am fairly new to ModX. where would I find the error log?
          • 41364
          • 9 Posts
          NVM on where to find the error log. Found it under reports lol. I need a nap.


          I cleared the log and re did everything. here is there error I am getting.

          [2012-10-31 15:36:54] (ERROR @ /ModX/index.php) Could not load class: Custom from mysql.custom. [ed. note: adam87 last edited this post 13 years, 11 months ago.]
            • 16348
            • 64 Posts
            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
            
              • 41364
              • 9 Posts
              Quote from: Kristoffer at Nov 01, 2012, 06:26 AM
              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
              

              I am learning quite a bit from this. So close now. Now it does redirect when done. But data is still not added to the custom table. here are my current errors.

               [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
              


              Could it be my MySql version
              --
              mysql Ver 14.14 Distrib 5.5.24, for debian-linux-gnu (x86_64) using readline 6.2
                • 41364
                • 9 Posts
                Thanks for all the help. I was able to get it to work after some more searching. I had to give my custom table the modx_ prefix and use bob's snippet accordingly
                [[!CreateXpdoClasses? &myPackage=`users` &myPrefix=`modx_users_` &myTables=`modx_users_Custom`]]