We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37686
    • 94 Posts
    How can i arange a manual order on a result of users, where i display the users based on an extended users attribute (for example the extended attribute instruments)?

    so in fact i want to prioritize users based on nothing static but based on manual input.
      • 40706
      • 128 Posts
      Sorting by an extended users attribute would be slow. The extended field is saved as an JSON Object in one database field. MySQL cant access this easily. The only way to do that directly in the database would be an ORDER BY with the use of substring and instr.

      Like this one:
      $cond = $modx->newQuery("modUserProfile");
      $cond->sortBy('SUBSTRING(`extended`, INSTR(`extended`, \'instruments":\')+13, 30)');
      
      $users = $modx->getCollection('modUserProfile', $cond);


      If it`s an more complex sorting of users... then it will be dam slow. Then MODx has to generate each object and sort it within PHP after unpacking all of the extended fields. [ed. note: m.engel last edited this post 13 years, 8 months ago.]
        • 9207 ☆ A M B ☆
        • 2,475 Posts
        The more "correct" solution (architecturally), would be to create another table for extra data and join it on the user id (1 to 1). This would make the database query much easier for reporting and sorting, but you'd end up having to customize some of the forms that store user data, and that could be tricky (serious devs only). Extended attributes are easy to implement, and there's a simple editing interface built in that will let you edit all that data, but the drawback is that it's very difficult to do sorting at the database level when data is stored like that. The other option would be to sort the data at the PHP level.
          • 3749
          • 24,544 Posts
          Remember that user extended fields are not only in a JSON object, they are in a different table than the users. A query on the modUser object won't get you much of value except the user id and username. http://bobsguides.com/modx-object-quick-reference.html#modUser

          You'd have to either query the modUserProfile object (if you know the user ID, or don't care about it), or do a join between the modUser and modUserProfile.

          It's possible to do that with $modx->getCollectionGraph(), but as Michael Engel says, you'd need a funky where clause to grab the objects you want.

          If speed is an issue, one fast method is to store the information in an unused field of the modUserProfile object (e.g., phone, fax, country, etc.). That makes everything simple and quite fast, but as Everett says, not architecturally correct. Be careful of the length of each field, which you can see in the fieldmeta section here: http://bobsguides.com/modx-object-full-reference.html#modUserProfile

          Another way to go, if you won't be using the user messaging system in the Manager for anything, is to use that table to store everything (using a plugin connected to something like the OnUserFormSave event to update it). http://bobsguides.com/modx-object-quick-reference.html#modUserMessage. You could put the user's userId in the 'recipient' field and whatever you want in the 'subject' and 'message' fields. I've never done that, but can't see why it wouldn't work, assuming that the fields are long enough for what you want (subject is 255, and 'message' is a text field, so lots of room there).

          Of course, the 'correct' method is to create your own custom table containing whatever information you need as Everett suggested. Making it xPDO-friendly is a great help, and there's a utility to help you do that here: http://bobsguides.com/custom-db-tables.html. Then you could use $modx->getCollectionGraph() to get everything you want, sorted however you want in a single query to the DB.
            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
            • 40706
            • 128 Posts
            Oh yes, just typed in here untested. Of corse it is the modUserProfile.