We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 48171
    • 6 Posts
    Hi All,

    I am trying to create an array for use in a datatable list that shows the users of a group and a related user (e.g. supervisor)

    In the UserProfile, I am using the Comment field to hold the ID of the users supervisor and would like to be able to create a list that will include the supervisors name.

    The list includes the following fields:
    StudentID, Fullname, Email, Phone, Supervisor

    At the moment, I am using the following code:
        foreach ($users as &$user) {
            $tmpArr = "";
            $profile = $user->getOne('Profile');
            $_user = $profile->get('extended');
    
    // Create a variable to add to the array below
            $tmpAgent = '';
    
            if ($profile->get('comment') == '') {
                $tmpAgent = '';
            }
            else {
    // If I use this then nothing seems to get returned. no rows at all.
            $_agent=$modx->getObject('modUser',$profile->get('comment'));
            $_agentProfile = $_agent->getOne('Profile');
            $tmpAgent=  $_agentprofile->get('fullname');
    
    //  If I use this the ID gets returned e.g. 4344
    //        $tmpAgent=  $profile->get('comment');
    //
    
            }
            $tmpA = '<a href="' . $modx->makeUrl(34) . '?studentId=' . $user->get('id') . '">';
            $tmpArr[] = $tmpA.$user->get('id') . '</a>';
            $tmpArr[] = $tmpA.$profile->get('fullname')."</a>";
            $tmpArr[] = $tmpA.$profile->get('email') . "</a>";
            $tmpArr[] = $tmpA.$_user['address']['mobile'] . "</a>";
    
            $tmpArr[] = $tmpA.$tmpAgent . "</a>";
    
            $tmpArray[] = $tmpArr;
            unset($tmpArr);
            unset($profile);
            unset($_user);
            unset($_agentProfile); 
            unset($_agent);
    


    Can anyone suggest a better way or more efficient way to query the fullname of the Supervisor?

    Thanks in advance.
      • 3749
      • 24,544 Posts
      Try this:

      $_agent=$modx->getObject('modUser', (int) $profile->get('comment'));


      There are some things you could do to speed this up. First, you can use getCollectionGraph() to get the user and the profile in a single query. (In theory, you could get the supervisor's record at the same time, but I don't think it's worth the effort.) This will get all active users and their profiles:

      $c = $modx->newQuery('modUser');
      $c->sortby('fullname', 'ASC');
      $c->where(
      array('modUser.active' => '1'),
      );
      $users = $modx->getCollectionGraph('modUser', '{"Profile":{}}', $c);

      Once you have the user and profile, you can create your array like this:

      foreach ($users as $user) {
          $tmpArray = array_merge($user->Profile->toArray(), $user->toArray());
          /* add your extra fields here */
      }
      


      Then, create a Tpl chunk with placeholders for the fields and do this:

      $output .= $modx->getChunk('MyChunk', $tmpArray;


      Finally, here is a much faster way to get the supervisor's name:

      $query = $modx->newQuery('modUserProfile', array(
          'internalKey' => (int) $user->Profile->get('comment'),
      ));
      $query->select('fullname');
      $tmpAgent = $modx->getValue($query->prepare());
      

        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
        • 48171
        • 6 Posts
        Awesome! Thanks for that!
          • 3749
          • 24,544 Posts
          Don't thank me until it works. wink
            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