We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 35756
    • 166 Posts
    Hello again!

    Now that the whole Login-thingy is runnig quite well including some extended fields for each user-profile I need to be able to search inside those extended-fields. How can I achieve that?

    The forum here contains some few threads where others asked for this, but I couldn't find a solution so far. I already installed AdvSearch via the Package Management and tried to get the values of the extended fields, but this doesn't seem to work.

    By creating a resource "Userprofile [[+username]]" I have a single ressource in which a snippet is called which gets the requested user-field-data of the "normal" and the extended fields. So there are no different values for each user, just this single one. I guess the AdvSeach-Package only works for different resources?

    Any way to get this done?

    This question has been answered by multiple community members. See the first response.

      • 3749
      • 24,544 Posts
      The user extended fields are stored as a JSON string in the 'extended' field of the user profile. Searching them is problematical at best unless you retrieve every user profile, extract the extended fields, then search the array of extended fields. Here's an example of code that would do that. In the example, it's assumed that the key of the extended field you want is 'color' and you're searching for 'red':

      $key = 'color';
      $searchTerm = 'red';
      
      $profiles = $modx->getIterator('modUserProfile') 
      
      foreach($profiles as $profile) {
          $extended = $profile->get('extended');
          if ($extended[$key] == $searchTerm) {
              /* found one -- do something */
          }
      }


      As you might guess, this is quite slow. If (and it's a big if), your search term is unique enough, you can search using a LIKE query on the 'extended' field of the modUserProfile object.

      Really, though, you should consider using ClassExtender to extend the modUser object. That will let you put all your data in a custom table. You can then search and sort with blinding speed in a single query. The User Profile's extended fields are really not designed for searching and sorting.
        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
        • 35756
        • 166 Posts
        Thx again Bob!

        By doing some more research on this yesterday I found those two threads which I thought might be leading to a solution:

        formit2db with autocreate schema&classes
        http://forums.modx.com/thread/?thread=32560&page=

        Extending modUser
        http://rtfm.modx.com/revolution/2.x/developing-in-modx/advanced-development/extending-moduser

        So I'm going to give the modUser-extend-way a try on the weekend!
        • discuss.answer
          • 3749
          • 24,544 Posts
          Doing it with ClassExtender will save you a lot of time and trouble, though it takes a slightly different approach than the rtfm method.

          http://bobsguides.com/classextender-class.html

          http://bobsguides.com/blog.html/2014/05/27/why-extend-moduser/
            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
            • 35756
            • 166 Posts
            And again a big thank you to you!

            Gonna have a look at your posted links and try it this way!
              • 35756
              • 166 Posts
              Hello again!

              Sorry for asking again...I installed the ClassExtender-Package via the Package-Management (in MODx 2.2.15) which worked fine.

              Then I wanted to access the newly created ressource "Extend modUser" in the front-end, but I only get an

              "Unauthorized Access"

              message in the front-end.

              In the backend I did get those error-messages:

              [2014-09-14 16:12:32] (ERROR @ /mysite/manager/index.php) userData::load() is not a valid static method.
              [2014-09-14 16:12:32] (ERROR @ /mysite/manager/index.php) Could not load class: userData from mysql.userdata.

              I did activate/publish the resource "Extend modUser" and I activated the ExtraUserFields-Plugin. What else am I doing wrong? I first wanted to have a look at your example-resources and the forms that should be shown (if I understand your linked ClassExtender Class Tutorial correct).
                • 35756
                • 166 Posts
                Still no success to get this running...I flushed the session and permissions, deleted the cache via FTP, but no success, always "Unauthorized Access".

                I commented out the lines that are responsible for the die-error in the ClassExtender-snippet. Now I can see the form, but on submit I get the following error:

                Could not open schema file

                Also I did read often about a system-setting called "extension_packages". Should this been added by installing the ClassExtender-Package? Because I don't get any system-setting added?
                  • 35756
                  • 166 Posts
                  Ahoi again...

                  I tried a lot last night by keeping this mentioned line of code disabled in the ClassExtender-Snippet:

                  //    die ('Unauthorized Access');


                  Then I could see the form in the Extend modUser-resource. I created the table by submitting the form and looked at the examples and to several posts in this forum.

                  After that I managed to edit the chunks and nearly everything like I need it to be, all values get saved in the db-table and listed.

                  Now I'm having trouble with the UserSearchForm-Snippet. I want to have a single text-input-field to search in all fields of the table, not in a single one. In the example is one field for the first name, one for last name. I managed to edit this for some checkboxes I'm using.

                  $pFirstName = $modx->getOption('user_search_vorname', $_POST, '');
                  $pLastName = $modx->getOption('user_search_nachname', $_POST, '');
                  $pKeyWords = $modx->getOption('user_search_keywords', $_POST, '');
                  $pKeyWords2 = $modx->getOption('user_search_keywords2', $_POST, '');
                  
                  $modx->setPlaceholder('user_search_vorname', $pFirstName);
                  $modx->setPlaceholder('user_search_nachname', $pLastName);
                  $modx->setPlaceholder('user_search_keywords', $pKeyWords);
                  $modx->setPlaceholder('user_search_keywords2', $pKeyWords2);
                  
                  $fields = array();
                  
                  if (isset($_POST['submit-var']) && $_POST['submit-var'] == 'etaoinshrdlu') {
                  
                      $fields['where'] = '{"vorname:=":"' . $pFirstName . '","OR:nachname:=":"' . $pLastName . '","OR:keywords:=":"' . $pKeyWords . '","OR:keywords2:=":"' . $pKeyWords2 . '"}';
                  
                      $results = $modx->runSnippet('GetExtUsers', $fields);
                  
                  }


                  But my form/profile has additionally about 25 different text-inputs and some textareas, which I'd like to search at once. I don't understand how I can access all values of every field in my ext_user_data-table?

                  Edit: I realized the search only works for exact matches. Is there a way to search in "wildcard"-style, for example searching for "marc" gives an output of 2 entries (for 2 user accounts containing "marc" and "marcus")? Right now by using "marc" you only get one result, you have to search exactly for "marcus" to get this as the result... [ed. note: profilneurotiker last edited this post 9 years, 7 months ago.]
                    • 4172
                    • 5,888 Posts
                    But my form/profile has additionally about 25 different text-inputs and some textareas, which I'd like to search at once. I don't understand how I can access all values of every field in my ext_user_data-table?

                    Doesn't it work, how you allready did it?
                      -------------------------------

                      you can buy me a beer, if you like MIGX

                      http://webcmsolutions.de/migx.html

                      Thanks!
                      • 4172
                      • 5,888 Posts
                      you would need LIKE - searches:

                      if (isset($_POST['submit-var']) && $_POST['submit-var'] == 'etaoinshrdlu') {
                       
                          $fields['where'] = '{"vorname:LIKE":"%' . $pFirstName . '%","OR:nachname:LIKE":"%' . $pLastName . '%","OR:keywords:LIKE":"%' . $pKeyWords . '%","OR:keywords2:LIKE":"%' . $pKeyWords2 . '%"}';
                       
                          $results = $modx->runSnippet('GetExtUsers', $fields);
                       
                      }
                      
                        -------------------------------

                        you can buy me a beer, if you like MIGX

                        http://webcmsolutions.de/migx.html

                        Thanks!