We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37258
    • 34 Posts
    I'm creating a snippet that queries a table I've added to the ModX database. All I want to do initially is select the user group of the logged in account, create a select statement incorporating the group info and then return the query to the screen to check the correct values have been entered. However I can't seem to get the text of the query to dump to the screen.

    I've tried various methods - var_dump, print_r, echo - they all return nada. Is there something I'm missing?

    Additionally, why is the usergroup returning as an array? I expected just the ID of the group the logged in account belongs to - and it is in the array - I'm just wondering what the other integers represent...

    // Check if user is logged in and get user group 
    $user = (!empty($userId)) ? $modx->getObject('modUser', $userId) : $modx->user;
    
    if (is_object($user)) {
        /*
        * modUser Groups
        */
        $group = print_r($user->getUserGroups(), true);
        $modx->toPlaceHolder('user.groups', $group);
    }
    
    // Define vars
    if($_SERVER['REQUEST_METHOD'] == "POST") {
    
        if ($_POST['submit'] == 'Submit') {
            $accNo = $_POST['accNo'];
            $accName = $_POST['accName'];
            $saleProperty = $_POST['saleProperty'];
            $clientContact = $_POST['clientContact'];
    } 
    
    // Initial query against user group
    echo $group;
    $query = $DBH->query("
        SELECT * FROM whc_relationships
        WHERE UGroup = `$group`
    ");
    
    // Print Query for debugging
    echo $query;
    }
    
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      Users can belong to more than one group.

      Something like
      print $query->toSQL();


      See also https://forums.modx.com/thread/31991/how-to-debug-xpdo-sql-queries
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 3749
        • 24,544 Posts
        If that doesn't work, try:

        $query->prepare();
        print $query->toSQL();


        BTW, I think this would be a lot simpler:

        $groups = array();
        if (is_object($modx->user)) {
        $groups = $modx->user->getUserGroups();
        }
        return (print_r($groups, true));
        


        getUserGroups() returns an array containing the IDs of every group the user belongs to. There is also getUserGroupNames() if that would work better for you.

        In addition, each user can have a primary user group (set on the Create/Edit User panel) and available with:

        $group = $modx->user->getPrimaryGroup();


        Unlike the first two methods, it returns the modUserGroup object, so you need a next step:

        $groupName = $group->get('name');
        $groupId = $group->get('id');
        





          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
          • 37258
          • 34 Posts
          Quote from: BobRay at Jun 04, 2014, 04:02 PM
          If that doesn't work, try:

          $query->prepare();
          print $query->toSQL();


          BTW, I think this would be a lot simpler:

          $groups = array();
          if (is_object($modx->user)) {
          $groups = $modx->user->getUserGroups();
          }
          return (print_r($groups, true));
          


          getUserGroups() returns an array containing the IDs of every group the user belongs to. There is also getUserGroupNames() if that would work better for you.

          In addition, each user can have a primary user group (set on the Create/Edit User panel) and available with:

          $group = $modx->user->getPrimaryGroup();


          Unlike the first two methods, it returns the modUserGroup object, so you need a next step:

          $groupName = $group->get('name');
          $groupId = $group->get('id');
          



          Excellent, thanks very much Bob and Sue, your knowledge is invaluable.