1

I am writing a custom module that takes some old data and stores it into a new user profile. However, when the user_save function is ran, I get the errors:

Warning: array_flip(): Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 175 of /.../includes/entity.inc).

EntityMalformedException: Missing bundle property on entity of type node. in entity_extract_ids() (line 7844 of /.../includes/common.inc).

After some troubleshooting, I realized that these errors were being thrown when I ran user_save('', $user_fields). The code that writes $user_fields is as follows:

    <?php
function users_migration_form_submit($form, &$form_state) {
    $id = $form_state['values']['memberId'];
    $password = $form_state['values']['password'];

    $other_database = array(
        'database' => 'old_users',      // Name/Schema
        'username' => 'old_users',      // User
        'password' => '',               // User password
        'host' => '127.0.0.1',          // DB Host
        'driver' => 'mysql',            // DB Driver
    );

    Database::addConnectionInfo('users_legacy', 'default', $other_database);
    db_set_active('users_legacy');

    //Enter in queries here
    $query = "SELECT * FROM Users WHERE password=PASSWORD('".$password."') AND memberID='".$id."'";

    $result = db_query($query);

    $user_fields = array();

    if($result) {
        //Build user variable
        $user_fields['name'] = $id;
        $user_fields['pass'] = $password;

        while($row = $result->fetchAssoc()) {
            $user_fields['mail'] = $row['email'];
            $user_fields['field_user_gender'][LANGUAGE_NONE][0]['value'] = strtolower($row['gender']); // Example: male
            $user_fields['field_user_dob'][LANGUAGE_NONE][0]['value'] = date('Y-m-d', strtotime($row['dob'])); // This is a date field. The incoming data will look like this: 01/01/1990
            $user_fields['field_user_id'][LANGUAGE_NONE][0]['value'] = $row['user_id']; // Example: loganfarr
            $user_fields['field_rank'][LANGUAGE_NONE][0]['value'] = $row['rank']; // Example: 10B
        }
    }
    else {
        print_r("No results.");
        return;
    }

    $user_fields['status'] = 1;

    db_set_active(); //Set it back to so Drupal uses the default database

    try {
        $account = user_save('',$user_fields);
    } catch (EntityMalformedException $e) {
        drupal_set_message('EntityMalformedException occured', 'error');
        dpm(debug_backtrace());
        return;
    }

    //Automatically log in as the newly created user
    if($uid = user_authenticate($id, $password)) {
        global $user;
        $user = user_load($account->uid);

        $login_array = array('name' => $id);
        user_login_finalize($login_array);

        drupal_goto('/user/edit');
    }
    else {
        print_r('user_authenticate() failed');
        return;
    }
}

After I ran kpr($user_fields) I realized that the array was populating correctly, and all the values were there.

All the fields are text fields with the exception of the field_user_dob, which is a date field.

I have tried using the entity_save function (and respective setting functions via entity_metadata_wrapper) but I end up getting the same errors. I can provide that code as well, if necessary.

Thank you in advance!

EDIT I cleaned up the code for consistency. $old_user and $row were the same variable, just mis-typed when I was abstracting the code for this question.

The output of kpr($user_fields) is as follows:

... (Array, 9 elements)
    name (String, 10 characters ) FAR1234567
    pass (String, 8 characters ) password
    mail (String, 28 characters ) logan@loganfarr.com
    gender (String, 4 characters ) male
    dob (Integer) 790326000
    user_id (String, 10 characters ) 0987654321
    field_rank (String, 2 characters ) 10B

UPDATE

I have tried removing the custom fields so that only the essentials are there (name, pass, and mail) and still receive the errors.

UPDATE #2

I added the whole function, not just the lines relevant to user_save.

UPDATE #3

(Thanks to all who are still here with me.)

I have removed all other code and have just attempted

$user_fields = array();
$user_fields['mail'] = 'logan@loganfarr.com';
$user_fields['name'] = 'FAR1234567';
$user_fields['pass'] = 'password';
$account = user_save(null,$user_fields);

Which still gives me the error.

  • Hello again @loganfarr. I'm looking at your code - I'm not too savvy about php, but there are a couple of things I noticed. Since we are kind of reaching the limit of logic here, maybe this is something you want to try? :) (1) I noticed that you still are using $old_user in the code, but this array is not defined anywhere that I can see (see line "$user_fields['field_rank'][LANGUAGE_NONE][0]['value'] = $old_user['rank'];"), and (2) the function user_save needs an OBJECT as the first argument. Many online post use it as user_save(null, $edit);. PERHAPS your choice of using '' is causing it – argiepiano Oct 26 '15 at 19:45
  • In other words, instead of using user_save('', $user_fields) try user_save(null, $user_fields); – argiepiano Oct 26 '15 at 19:47
  • @argiepiano I have fixed the $old_user thing. Another thing I missed while cleaning up the abstraction. Sorry about that. Also I tried using user_save(null, $user_fields) and it is still throwing an error. – loganfarr Oct 26 '15 at 20:07
  • I'm puzzled by the fact that your error says ""Missing bundle property on entity of type node." I think you be more drastic in your debugging. Can you start a clean new site with JUST this custom module (No Rules, no other content types, no other modules, nothing else) and test? The other thing that may help potential "helpers" is for you to take a snapshot of your User "Manage fields" screen so that people can see the field types etc. – argiepiano Oct 27 '15 at 16:41
0

This line is probably the problem:

$user_fields['field_verified'] = 1;

Assuming that's a boolean field it should be:

$user_fields['field_verified'][LANGUAGE_NONE][0]['value'] = 1;
  • I tried this and it did not yield any different results. – loganfarr Oct 25 '15 at 7:52
0

Try this way:

$user_fields = array();
//Build user variable
$user_fields['name'] = $id;
$user_fields['pass'] = $password;
$user_fields['mail'] = $row['email'];
$user_fields['field_user_gender'] = strtolower($row['gender']);
$user_fields['field_user_dob']= date('Y-m-d', strtotime($row['dob'])); 
$user_fields['field_user_id'] = $row['user_id'];
$user_fields['field_rank'] = $old_user['rank']; 
$user_fields['status'] = 1;
$user_fields['field_verified'] = 1;
$account = user_save('', $user_fields);

The array that you are passing should be an array of fields and values to save. For example array('name' => 'My name'). Key / value pairs added to the array will be serialized and saved in the {users.data} column.

UPDATE: Are you sure that each value is string or integer? In the piece of code that you displayed, you don't need [LANGUAGE_NONE][0]['value'] at least not for the $user_fields array. In fact you're declaring $user_fields as empty array at the begining.

But maybe $row or $old_user needs it, I'm not sure from where those are coming from. You have to make sure that you are assigning only one value and not an array. Check those variables using:

echo "<pre>"; 
print_r($row); 
print_r($old_user); 
echo "</pre>"; 

Less probably, but also make sure that $id or $password is not an array.

  • I had tried this originally and it still gave me the error. I read somewhere that it was because I was missing the [LANGUAGE_NONE][0]['value'] arrays which is why I put them in the first place. – loganfarr Oct 25 '15 at 7:56
  • Are you sure that each value is string or integer? In the piece of code that you displayed, you don't need [LANGUAGE_NONE][0]['value'] at least not for the $user_fields array. But maybe $row or $old_user needs it, I'm not sure from where those are coming from. To verify if you are assigning only a value, check those variables using: echo "<pre>" print_r($row); print_r($old_user); echo "</pre>"; – Blanca Esqueda Oct 25 '15 at 13:59
  • $old_user and $row are actually the same array. I mistyped when abstracting the code. I have fixed the code and added the output of kpr($user_fields) – loganfarr Oct 25 '15 at 17:50
  • Ok, then replace your variables with fixed values and try to save the user. Once you have it saved successfully, then start adding back your variables one by one until you discover the faulty one. – Blanca Esqueda Oct 25 '15 at 18:48
  • After hard coding the expected values, I still get the above errors. I even started removing the fields from the script so that just the essentials were being saved (name, pass, mail). Still got the errors :( – loganfarr Oct 25 '15 at 18:57
0

Notice that in your variable dump dob is actually a UNIX timestamp (an Integer). In your code the function date() returns a formatted date string (a string like "2015-10-25"), not an Integer timestamp.

Check the format of $old_user['dob'] by doing a dpm() of that variable. IF it shows as Integer (i.e. UNIX timestamp), you simply do this: $user_fields['field_user_dob'] = old_user['dob'];

If it's a date string, then you would need to add strtotime($old_user['dob']) but not date().

The other thing: I think you were correct to add [LANGUAGE_NONE][0]['value'] as you had it before. You may need to add that back (I'd check on that but I'm short on time right now).

  • After checking that the date field was being handled properly (it is), and adding the [LANGUAGE_NONE][0]['value'], I am still getting the original errors. – loganfarr Oct 25 '15 at 18:29
  • When you say "a new user profile" in your original post, do you mean you are using the module profile2 to store the profile? And, perhaps it would be useful to post the structure of your user object by running the following in /devel/php: $user=user_load(1); dpm($user); – argiepiano Oct 25 '15 at 19:16
  • Saw your latest update. I'm starting to suspect the error is coming from somewhere else in your code, within that function. Can you pls post the whole function? This line in your error is fishy: "Missing bundle property on entity of type node." Why would it refer to an "entity of type node" if you are working with entity type "user"?? – argiepiano Oct 25 '15 at 19:31
  • I added the rest of the function's code. The $id and $password variables are loading correctly, and the function is getting loaded. Thank you and thanks to @Blanca-Esqueda for the continued assistance! – loganfarr Oct 26 '15 at 15:47
0

Okay, after some really in-depth troubleshooting, I have finally found the problem.

When a user registers, there is a rule that fires that assigns them a role based on the answer to a field (a list field that is required). I was not adding that field in the code, so the rule had nothing to work with.

There wasn't anything in the logs, code, or debug_backtrace() that pointed to the problem. It was only when I looked at the overall user flow that I thought of it.

Many thanks to all who helped. I really appreciated it.

  • Great! I think I mentioned the Rules in one of the comments above. I'm glad you found the culprit. – argiepiano Oct 27 '15 at 20:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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