We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 11927
    • 105 Posts
    How do you save information for a user when it is part of the user’s profile?

    Meaning, how do I change the user’s fullname, email, etc. but specifically in a plugin?

    So far I have this for OnUserBeforeSave, but it doesn’t change anything in the database:
    <?php
    $profile = $user->getOne('Profile');
    $profile->set('fullname', 'change fullname');
    $user->addOne($profile, 'Profile');
    $user->save();




    Solution:
    If wanting to save $user profile, then need to getObject.

    Correct code:
    <?php
    $user_edit = $modx->getObject('modUser', $id);
    $profile_edit = $user_edit->getOne('Profile');
    $profile_edit->set('fullname', 'change fullname');
    $user_edit->addOne($profile_edit, 'Profile');
    $user_edit->save();
      You may or may not want to use the code I write. It&#39;s probably all against the syntax rules of php and MODx. smiley

      Carpet Cleaning
      • 22303 MODX Staff
      • 10,725 Posts
      A more concise way to do this, without the unnecessary addOne() call, which would only need to be called if you were creating the Profile when creating the User.
      <?php
      $user = $modx->getObject('modUser', $id);
      if ($user && $user->getOne('Profile')) {
          $user->Profile->set('fullname', 'new fullname');
          $user->Profile->save();
      }
      
        • 11927
        • 105 Posts
        Thanks.

        I’ve always wondered how to get so I can just use $user->Profile.
          You may or may not want to use the code I write. It&#39;s probably all against the syntax rules of php and MODx. smiley

          Carpet Cleaning