4

I create a user from a form (that part works fine) but no profile2 information seems to be created....

profile2_load_by_user returns an empty array which results in the below error. Do I need to some other step to first associate an "ae_sa" profile with the newly created user?

Recoverable fatal error: Argument 1 passed to profile2_save() must be an instance of Profile, instance of stdClass given, called in D:\FULLREPO\trunk\sites\all\modules\sps_users\sps_users.module on line 115 and defined in profile2_save() (line 291 of D:\FULLREPO\trunk\sites\all\modules\profile2\profile2.module).

...
$account = user_save(null, $new_user);
$obj_user = user_load($account->uid) ;
$profile2 = profile2_load_by_user($obj_user) ;
$profile2["ae_sa"]->field_full_name[LANGUAGE_NONE][0]["value"] = $fullname;
$profile2["ae_sa"]->field_title_role[LANGUAGE_NONE][0]["value"] = $titlerole;
$profile2["ae_sa"]->field_company[LANGUAGE_NONE][0]["value"] = $ucompany;
$profile2["ae_sa"]->field_bio[LANGUAGE_NONE][0]["value"] = $bio;
profile2_save($profile2["ae_sa"]) ;
2
  • You using Drupal7? If so, Why not use the field-able profile fields that come "out-of-the-box"? Commented Aug 7, 2012 at 14:47
  • Working on a project begun by someone else, switching at this point is not an option
    – Jim
    Commented Aug 7, 2012 at 15:06

2 Answers 2

5

You need to create the new user's profile:

$profile = profile2_load_by_user($user->uid, 'ae_sa');
if(empty($profile)) {
  $profile = profile2_create(array(
    'type' => 'ae_sa',
    'uid' => $user->uid,
  ));
}

// Fill fields
$profile->field_full_name[LANGUAGE_NONE][0]["value"] = $fullname;
$profile->field_title_role[LANGUAGE_NONE][0]["value"] = $titlerole;
$profile->field_company[LANGUAGE_NONE][0]["value"] = $ucompany;
$profile->field_bio[LANGUAGE_NONE][0]["value"] = $bio;

// Save the newly created OR updated profile
profile2_save($profile); 
1
  • this is perfect answer, when user dont have profile data that time you are not able to load profile and save data , thats time you have to use $profile = profile2_create(array( 'type' => 'ae_sa', 'uid' => $user->uid, )); Commented Mar 14, 2017 at 14:42
3

You need a second variable for the profile2_load_by_user($obj_user) function. What you currently get back is an array of profiles, e.g

'candidate' => Profile object,
'person' => Profile object,
'student' => Profile object

You need to specify the profile in the call and then you just get a profile object back. Then you can save the profile as it is an Profile object.

e.g. profile2_load_by_user($obj_user,'candidate');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.