• newObject("modUser") -OK, but how add `_user_attributes` fileds?#

  • modx.customize Reply #1, 3 months, 2 weeks ago

    Reply
    Hi

    I managed to add new users with this code:

    $userTmp = $modx->newObject("modUser");
    	  $userTmp->set('username', $lender);
    	  $userTmp->set("password", md5($pass));
              $userTmp->save();
    	  $idUser = $userTmp->get('id');


    This add user to Manger area but only to table : *prefix*users
    Then I add :
    	  $_usr = $modx->getObject('modUser',$idUser);
    	  if (!$_usr) return 'no user??';
    	  $_profile = $_usr->getOne('Profile');  
    	  $_profile->setValue('email', 'no-reply@demo-site.com');  
    	  $_profile->save();


    I am sure I get this crashed ((

    I need to populate *prefix*user_attributes table cause I think this must be updated / synchronised together with the first table but my first part code does not do it. That's why i am trying to use Profile and SET..

    Probably I should not use profile and set method? Or I use it wrong in terms of users updates?

    Thanks


  • blankcheck Reply #2, 3 months, 2 weeks ago

    Reply
    $userTmp = $modx->newObject("modUser");
    $userTmp->set('username', $lender);
    //you don't need md5
    $userTmp->set("password", $pass);
    $profile = $modx->newObject("modProfile");
    $profile->set('email', 'no-reply@demo-site.com');
    $userTmp->addOne($profile);
    $userTmp->save()
    


  • modx.customize Reply #3, 3 months, 2 weeks ago

    Reply
    Hi

    thanks a lot.

    You are right I do not need to md5 it makes it as I see now when followed your note, thanks.

    I checked the structure of users table and the pass column is not auto md5 so how the string gets md5 , please?

    On your line 6: $profile->set('email', 'no-reply@demo-site.com');
    The script dies. Do you kn0w what is wrong with this method?

    Here is my script now:

    	 
              $userTmp = $modx->newObject("modUser");
    	  $userTmp->set('username', $lender);
    	  $userTmp->set("password", $pass);
     	  $profile1 = $modx->newObject("modProfile");
    // 	  $profile1->set('email', 'no-reply@demo-site.com');
    	  $userTmp->addOne($profile1);
    	  $userTmp->save();
    


    But it still does not add any data to second table **atributes .. Is it due to no field for that table was set? Could you suggest what is wrong with line 6 please?

    Thanks a lot.


  • blankcheck Reply #4, 3 months, 2 weeks ago

    Reply
    $userTmp = $modx->newObject("modUser");
    $userTmp->set('username', $lender);
    $userTmp->set("password", $pass);
    //this should be modUserProfile, not modProfile as I wrote before, sorry
    $profile1 = $modx->newObject("modUserProfile");
    $profile1->set('email', 'no-reply@demo-site.com');
    $userTmp->addOne($profile1);
    $userTmp->save();
    


    If you still have problems, feel free to ask.

    P.S. As for md5, I haven't investigated it, but i suspect that modx does it internally when new user is saved. Also, modx adds salt to whole thing. md5 can be also set as a function on table cell (modx doesn't do it that way)


  • modx.customize Reply #5, 3 months, 2 weeks ago

    Reply
    Thanks for the fix - I get it working now ;-)

    And many thanks for commenting the md5 for pass in modx.

    Hope it will be usable for many people who import old data into modx with creating web users.