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

    I am trying to access the user fields profile values for a particular user with the following snippet code. I am not getting any profile output. Can somebody help it out?

    <?php
    $output=’’;
    $user = $modx->getObject(’modUser’,$userId);
    $output.=$user->get(’username’);

    $c = $modx->newQuery(’modUser’);
    $c->andCondition(’user’==$modx->getObject(’modUser’,$userId));

    $profile =$modx->user->getOne(’Profile’,$c);

    $output.=$profile->get(’fullname’);
    $output.=$profile->get(’email’);

    return $output;

    If I am trying this code without any condition condition($c), I am getting default admin values.
      • 28615
      • 14 Posts
      I got the above problem solved by the following code. Now this works if I give xPDO query with a number like "12". I want that to be variable like [[+UserId]]. Any suggestion? Thanks

      <?php
      $output=’’;
      $user = $modx->getObject(’modUser’,$userId);

      $c = $modx->newQuery(’modUserProfile’);
      $c->where(array(’id’=>’12’));

      $profile =$modx->user->getOne(’Profile’,$c);

      $output.=$profile->get(’fullname’);
      $output.=$profile->get(’email’);
      $output.=$profile->get(’city’);

      return $output;

      ?>
        • 28215
        • 4,149 Posts
        <?php
        $userId = $modx->getOption('userId',$scriptProperties,false);
        if (empty($userId)) return '';
        
        /* get user and profile by user id */
        $user = $modx->getObject('modUser',$userId);
        if (!$user) return '';
        $profile = $user->getOne('Profile');
        if (!$profile) return '';
        
        $userArray = array_merge($user->toArray(),$profile->toArray());
        
        $modx->toPlaceholders($userArray,'user');
        return '';
        


        Then after your snippet call:

        [[+user.email]]
        [[+user.fullname]]
        
        etc...
        


        Alternatively, you can use the ’Profile’ snippet in the ’Login’ Extra, since it already does all this, as well as faster and handles extended fields: http://rtfm.modx.com/display/addon/Login.Profile

        [[!Profile? &user=`12` &prefix=`user.`]]
        
        Email: [[+user.email]]
        Fullname: [[+user.fullname]]
        
          shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
          • 28615
          • 14 Posts
          Thanks a lot. It is working. I had used the following code. However "photo" field is not rendered i.e image is not coming out from profile.

          [[!Profile? &user=`12` &prefix=`user.`]]
          
          Email: [[+user.email]]
          Fullname: [[+user.fullname]]
          

            • 28883
            • 35 Posts
            @stmani, thanks for posting the question.
            @splittingred, thanks for that pointer.

            I'm using that as part of my code to update a user's extended fields with post data I'm getting back from paypal;)
            woohoo:)
              • 40343
              • 9 Posts
              This is very Helpful.

              But, do you now how can i set or update those fields?
                • 28883
                • 35 Posts
                Hi Chaniel,

                Check out the Profile snippet that splittingred mentioned...it really makes life easy. Take note of the "useExtended" parameter. If this is set, your form will automatically update the extended fields. If the fields don't exist for the user, they get automatically created. There is of course an UpdateProfile snippet too, which again can be set to work with extended fields.

                http://rtfm.modx.com/display/addon/Login.Profile
                http://rtfm.modx.com/display/ADDON/Login.UpdateProfile

                I hope this helps!
                  • 40343
                  • 9 Posts
                  Thanks supanick smiley
                    • 38878
                    • 255 Posts
                    Interestingly I have tried both methods (profile and custom snippet) to retrieve author's city and state within a quip thread and every listing shows the last author's values. Snippet is the example splittingred posted above. I have read that values retrieved through profile are cached, and so I tried a custom snippet.

                    Snippet code is:
                    <?php
                    $userId = $modx->getOption('userId',$scriptProperties,false);
                    if (empty($userId)) return '';
                     
                    /* get user and profile by user id */
                    $user = $modx->getObject('modUser',$userId);
                    if (!$user) return '';
                    $profile = $user->getOne('Profile');
                    if (!$profile) return '';
                     
                    $userArray = array_merge($user->toArray(),$profile->toArray());
                     
                    $modx->toPlaceholders($userArray,'thisUser');
                    return '';


                    And quip chunk is:
                    [[!getAuthorInfo? &userId=`[[+author]]`]]
                    <blockquote class="testimonial">
                      <p>[[+body]]</p>
                    </blockquote>
                    <div class="arrow-down"></div>
                    <p class="testimonial-author"><a href="mailto:[[+email]]">[[+name]]</a> | <span>[[+thisUser.city]], [[+thisUser.state]]</span></p>


                    You can see the thread at http://www.skipngofarm.com/about-us/. Scroll down to testimonials.

                    Any ideas would be great.
                      • 39932
                      • 483 Posts
                      @harvey:

                      When it comes to users, you have a couple of possibilities. Yes, this information is cached, but not when doing a direct query.

                      This code will always get the current user:
                         $user = $modx->user;
                      


                      This code will get any user you want:
                          $id = 1;
                          $user = $modx->getObject('modUser', $id);
                      


                      In regard to fields, there are normal user fields and extended user fields.

                      A normal user field is not an array, but a value. It is also in the Profile object.

                      // Get the user via one of the methods above...
                          $profile = $user->getOne('Profile');
                          $email = $profile->get('email');
                      
                          $value = '[email protected]';
                          $profile->set('email', $value);
                          $profile->save();
                      


                      Extended Fields are an array, so you access these fields as if they were an array. This is the code to get/set extended fields.
                          $profile = $user->getOne('Profile)
                          $extended = $profile->get('extended');
                      // Set an extended field
                          $extended['field1'] = 'joe';
                      // Send the extended back to the Profile
                          $profile->set('extended', $extended);
                      // Save the Profile
                          $profile->save();
                      


                      Generally, if these methods are not working, you have the wrong User object. This often happens to coincidentally be '0'. Make sure you are getting either a) the current user or b) the user via getObject(). I never happen to use array_merge, simply because it frustrates me. The DB behaves by only setting what you tell it to, so there is no need to array_merge in my experience.
                        Website: Extended Dialog Development Blog: on Extended Dialog
                        Add-ons: AJAX Revolution, RO.IDEs Editor & Framework (in works) Utilities: Plugin Compatibility List
                        Tutorials: Create Cross-Context Resources, Cross-Context AJAX Login, Template-Based Actions, Remove Extensions from URLs

                        Failure is just another word for saying you didn't want to try. "It can't be done" means "I don't know how".