We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38783
    • 571 Posts
    I am using Bob Rays ClassExtender to create additional fields for users. One of these fields is idCode.

    This field is populated when the user first registers. This is working fine.

    I would like a site administrator to be notified by email if any user creates an account using an idCode number that has been used before by another user. I do not wish to prevent the new user from setting up the account if they are using a duplicate idCode, I just want to know about it.

    So trying to break this down: I am hoping to trigger an event based on a duplicate being found in an ExtUser field.

    I have hit a complete brick wall with this and do not even have have any failed sample code to show.

    If anyone has any ideas I would be very grateful for your help.
      If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

      email: [email protected] | website: https://andytough.com
      • 3749
      • 24,544 Posts
      I don't know your data structure, but one way to approach it is to check for duplicates in a plugin attached to the OnUserActivate event.

      You will have the user object there as $user. You can do something vaguely like this:

      $userId = $user->get('id');
      
      /* Get userdata custom fields for the user here,
       * and set $idCode variable to their idCode.
       */
      
      /* Check for duplicates */
      $results = $modx->getCollection('user_data_or_whatever', array('idCode' => $idCode));
      if (count($results) > 1) {
          /* Send email here */
      }
      
      return '';


      You could speed this up by using $modx->getCount() rather than getCollection(), but you probably want the actual records to use in filling out the email.
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 38783
        • 571 Posts
        Hello Bob

        Thank you for responding. I think this may be beyond my abilities at the moment, but if I tell you what I have done so far perhaps you can point me in the right direction. Even if this is to advise me to finish my CodeAcademy php course before trying to do anything like this!

        The data I want to check for duplicates is in:
        Table: ext_user_data
        Column: idCode

        In this table there are also columns called:
        id - This contains an id number issued on creation of a new user
        userdata_id - This contains the MODX user id number, again this is issued on creation of a new user
        I think the above is all standard for Class Extender

        My main MODX table does not have any prefixes to the column names, just the default columns.

        Before trying to make a plugin that triggers an email I thought I would make a page that would show me what the code you wrote would find. I seem to be running into problems though.

        My code is as follows

        <?php
        $user = $modx->getUser();
        print "MODX User Name: ";
        echo $user->get('username');
        print "<br />";
        print "MODX User ID: ";
        echo $user->get('id');
        print "<br />";
        /* Get userdata custom fields for the user here,
         * and set $idCode variable to their idCode.
         */
         
        /* Check for duplicates */
        $results = $modx->getCollection('userData', array('idCode' => $idCode));
        
        echo 'Below is the output from the $results array<pre>'; print_r($results); echo '</pre>';
        
        if (count($results) > 1) {
            /* Send email here */
        	print "the result is more than one so duplicates found <br />";
        }
        
        if (count($results) < 1) {
            /* Send email here */
        	print "the result is less than one so <strong>no</strong> duplicates found <br />";
        }
        return '';


        This produces the following

        MODX User Name: userfour
        MODX User ID: 249
        Below is the output from the $results array

        Array
        (
        )

        the result is less than one so no duplicates found

        The user name and id are correct for the logged in user. But the results array is empty and the report that no duplicates have been found is wrong as there are duplicate idCode entries.

        Tinkering with the line that reads
        $results = $modx->getCollection('userData', array('idCode' => $idCode));
        

        has resulted in various error messages like
         [2] => Unknown column 'userData.userData_idCode' in 'where clause'


        If you have any advice I would be very grateful.

        Best wishes,

        Andy
          If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

          email: [email protected] | website: https://andytough.com
          • 3749
          • 24,544 Posts
          I think you might be confused about this line:

          $results = $modx->getCollection('userData', array('idCode' => $idCode));


          It's not meant to find duplicates in general (though that would be possible with different code). Because it's attached to confirm register, it's meant to see if there is a duplicate idCode that matches the idCode of the current user being registered.

          For it to work, you would need to set $idCode to the idCode of the registering user, and there would need to be a duplicate for that idCode somewhere. It occurs to me that the idCode might not be available at that point, so maybe this is a bad approach.

            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 38783
            • 571 Posts
            Thank you Bob.
              If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

              email: [email protected] | website: https://andytough.com