We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26503
    • 620 Posts
    Sean Kimball Reply #1, 12 years ago
    No idea what could be happening here, see the code below, it fails at the line $profile_data = $profile->toArray(); seemingly randomly, I double checked the ids against the database tables id and profiles, it should have been creating the profile object [the user did exist with a record in modx_user_attributes]

    there are about 20 users, only three were failing to create the object, I finally disabled those users and everything worked fine. So I tried to recreate one of the users... this is where it gets weird. Two other users started failing to create the profile object, existing users, not the one I had just created!

    No IDEA what could be happening to cause this! HELP!!


    $sql = "SELECT 
    		u.*, ua.*, us.*, ux.*, u.id as id, ua.id as uaid, us.id as usid, ux.id as uxid
    		FROM `modx_users` u 
    		LEFT JOIN `modx_user_attributes` ua ON ua.internalKey = u.id
    		LEFT JOIN `modx_gssi_user_ext_data` ux ON  ux.internalKey = u.id
    		LEFT JOIN `modx_session` us ON  us.id = ua.sessionid
    		WHERE ( ua.blocked = 0 AND u.active = 1 and u.id != 1) 
    		ORDER BY u.id asc ";
    
    $results = $modx->query($sql);
    
      if (is_object($results)) {
    
    	$users = $results->fetchAll(PDO::FETCH_ASSOC);
    
    	if(count($users) <= 0){
    	  
    	  return;
    
    	}
    
      }
    
      echo '<pre>';print_r($users);echo '</pre>';
    
    foreach ($users as $user) {
    
    
    	$uid = $user['id'];
    
    	$myuser = $modx->getObject('modUser', $uid);
    
    	$profile = $modx->getObject('modUserProfile', $uid);
    
    	if(!is_object($profile)){echo 'not an object for id ' . $uid;}
    
    	$profile_data = $profile->toArray();
    
    	$detail = $myuser->toArray();
    
    	$account = array_merge($profile_data, $detail);
    
    
    	if($myuser->isMember(array('Customers'))){ //Customers, Staff
    		$customeroutput .= $modx->getChunk('dashCurrentActiveUserRowTpl',$account);
    	}else{
    		$staffoutput .= $modx->getChunk('dashCurrentActiveStaffRowTpl',$account);
    	}
    
    	//echo '<pre>';print_r($staffoutput);echo '</pre>';
    	//echo '<pre>';print_r($customeroutput);echo '</pre>';
    
    
    }
      *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

      Sean Kimball CLP, CLS.
      Technical Director / Sr. Developer | BigBlock Studios
      ._______________________________________________.
      Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
      27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
      phone/fax: 905-426-5525
      • 3749
      • 24,544 Posts
      Try changing this:

      $myuser = $modx->getObject('modUser', $uid);
      $profile = $modx->getObject('modUserProfile', $uid);


      to this:

      $myuser = $modx->getObject('modUser', $uid);
      if ($myuser) { 
          $profile = $myuser->getOne('Profile');
      }
        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
        • 46886
        • 1,154 Posts
        Could I ask what the problem is more clearly? What I am curious about is what happens when the object is not created. Because you seem to indicate that the user object isn't created, but the user somehow continues to exist or is created, its a little unclear.

        In my Discuss forum installation, I find that a portion of users don't get activated. Right now i don't know if those people even receive the confirmation email, to be honest.

        Anyway I am certainly curious about the behavior you have experienced, if you are willing to share.
          • 10487 MODX Staff
          • 1,535 Posts
          Garry Nutting Reply #4, 12 years ago
          Just to further what BobRay is saying, a likely problem here is that the primary key of the modUserProfile table is not always going to match the user ID from modUser. To fetch the correct profile, you should be looking up against the internalKey in modUserProfile:

          $profile = $modx->getObject('modUserProfile', array('internalKey' => $uid));


          BobRay's method is better though as that relationship is handled automatically via getOne().
            Garry Nutting
            Senior Developer
            MODX, LLC

            Email: [email protected]
            Twitter: @garryn
            Web: modx.com
            • 26503
            • 620 Posts
            Sean Kimball Reply #5, 12 years ago
            @Garry ~ yes, Bob's way is kind of more correct, depends on the application. If you just need the profile no sense in taking up memory with the user object then use your way. [though I think [*think*] internalKey might be internal_key]

            @ bob ~ yes, that's fine, but the $myuser object does get created for each user [that is active and not blocked] so your example would still fail for whatever reason my code is failing!
              *** Not just websites, we also create signage, banners, print, trade show displays and more! ***

              Sean Kimball CLP, CLS.
              Technical Director / Sr. Developer | BigBlock Studios
              ._______________________________________________.
              Bigblock Studios http://www.bigblockstudios.ca Web site design & development.
              27-1300 King Street East. Box 167 Oshawa, Ontario L1H8J4 Canada.
              phone/fax: 905-426-5525
              • 3749
              • 24,544 Posts
              As Gary suggested, the reason your code may be failing is that it assumes that the ID of the user is the same as the ID of that user's profile, which is not necessarily the case. On a given pass, you may have the user object of one user and the profile of a different user. You could also be trying get or test for the existence of a profile that doesn't (and shouldn't) exist, or creating and/or adding data to a profile that isn't actually connected to the user you're currently editing.

              All this would cause the exact kinds of weirdness you're reporting.

              My code will always get the profile of the user you're editing, so it's probably worth a try. 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