We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 47594
    • 7 Posts
    I'm attempting to get a user's ID from the URL of a page containing his/her username. For instance, /team/people/john-smith/

    Here's my code:
    /*
    	 * Get user ID to then find articles made by this user
    	 */
    	$user = $modx->newQuery('modUser');
    	$user->where(array(
    		'username' => $author //(john-smith)
    	));
    	$author = $modx->getObject('modUser', $user);
    	
    	
    	
    	/*
    	 * Get articles made by this user
    	 */
    	$ar = $modx->newQuery('modResource');
    	$ar->where(array(
    		'createdby' => $author->id,
    		'class_key' => 'Article',
    		'parent'    => $parentID
    	));
    	$ar->sortby('publishedon', 'DESC');
    	$articles = $modx->getCollection('modResource', $ar);
    	
    	
    	
    	/*
    	 * Loop through all articles made by user
    	 */
    	foreach ($articles as $article) {
    		
    		
    		
    		/*
    		 * Get article template variable image
    		 */
    		$image = $article->getTVValue('articleimage');
    		$modx->setPlaceholders(array('articleimage' => $image));
    		
    		
    		
    		/*
    		 * Get all articles by author
    		 */
    		$allArticles .= $modx->getChunk('curiosityAuthor', $article->toArray());
    		
    		
    		
    	}
    
    
    //etc...




    My issue is I'm receiving an error ONLY when I am NOT logged in. My code returns a 404 error with a 503 error within like so:




    In my Error Logs I see this:
    "Instantiated a derived class modUser that is not a subclass of the requested class modResource"



    Is modUser object only accessible when logged in? (I'm reasonably new to ModX)

    If the answer is yes to above then how do I work around getting an ID of a user from a username with xPDO without running a custom query.

    Thanks!
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      I think this would be a better way -
      $user = $modx->getObject('modUser',array('username'=>$username));
      $id = $user->get('id');

        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
        I don't see why it should matter, but I think Susan is right, that accessing the $user object inside the modResource query is causing a problem. IOW, do this:

            $author = $modx->getObject('modUser', $user);
            $authorId = $user->get('id');     
             
             
            /*
             * Get articles made by this user
             */
            $ar = $modx->newQuery('modResource');
            $ar->where(array(
                'createdby' => $authorId,
                'class_key' => 'Article',
                'parent'    => $parentID
            ));


        It's also possible that this could work:

        'createdby' => $author->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
          • 47594
          • 7 Posts
          I've updated my code to each idea individually but none seem to have fixed the error I'm receiving.

          I'm still getting a 404/503 error when I try to access my page when I'm not logged in to the manager.

          I have no packages/plugins or snippets on the site that check for users being logged in or not as there is no log in system.

          Here's my updated code with one of the changes suggested:

          	/*
          	 * Get user ID to then find articles made by this user
          	 */
          	$user = $modx->getObject('modUser',array('username' => $author));
          	$authorID = $user->get('id');
          	
          	
          	/*
          	 * Get articles made by this user
          	 */
          	$ar = $modx->newQuery('modResource');
          	$ar->where(array(
          		'createdby' => $authorID,
          		'class_key' => 'Article',
          		'parent'    => $parentID
          	));
          	$ar->sortby('publishedon', 'DESC');
          	$articles = $modx->getCollection('modResource', $ar);



          Thanks a lot for your help thus far.
            • 3749
            • 24,544 Posts
            Have you changed any Context Access ACL entries? Especially for the anonymous user group?

            Are you running the code outside of MODX or in a snippet?
              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
              • 47594
              • 7 Posts
              I haven't change any Context Access ACL entries. All permissions are default to the ModX installation.

              No code is being run outside of ModX.

              I'm still lost on what really to look at next.

              I've since removed all my code and the issue still occurs so it's caused elsewhere. This is Something to do with Article Authors only as everything else works fine.

              Thanks again for your suggestions. In your experience, what would you say the next best move is to find this? I've been following the code through to no avail.

              Edit: I should also note that I have not fiddled with Articles package in any way. [ed. note: totallyjacko last edited this post 12 years, 2 months ago.]
                • 47594
                • 7 Posts
                Huzzuh!

                I'm going to bite the bullet and tell you what an utter idiot I am in case someone else gets this issue as well.

                I checked all resources were published EXCEPT for the parent Article. I just needed to publish the parent article.

                Thanks a lot for your help all.

                  • 3749
                  • 24,544 Posts
                  I'm glad you got it sorted. Thanks for reporting back.

                  We've all been idiots -- it's inevitable. 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