Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

array_flip():Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load()

I have recently migrated my module to Drupal7 (on PHP Version 5.3.1) and now I am getting following errors:

    * Warning: array_flip() [function.array-flip]: Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 178 of C:\Users\akulkarni\Desktop\xampp\htdocs\servicecasting\includes\entity.inc).
    * Warning: array_flip() [function.array-flip]: Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 178 of C:\Users\akulkarni\Desktop\xampp\htdocs\servicecasting\includes\entity.inc).
    * Warning: array_flip() [function.array-flip]: Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 178 of C:\Users\akulkarni\Desktop\xampp\htdocs\servicecasting\includes\entity.inc).
    * Warning: array_flip() [function.array-flip]: Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->cacheGet() (line 354 of C:\Users\akulkarni\Desktop\xampp\htdocs\servicecasting\includes\entity.inc).
    * Warning: array_flip() [function.array-flip]: Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 178 of C:\Users\akulkarni\Desktop\xampp\htdocs\servicecasting\includes\entity.inc).
    * Warning: array_flip() [function.array-flip]: Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->cacheGet() (line 354 of C:\Users\akulkarni\Desktop\xampp\htdocs\servicecasting\includes\entity.inc).
    * Warning: array_flip() [function.array-flip]: Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->load() (line 178 of C:\Users\akulkarni\Desktop\xampp\htdocs\servicecasting\includes\entity.inc).
    * Warning: array_flip() [function.array-flip]: Can only flip STRING and INTEGER values! in DrupalDefaultEntityController->cacheGet() (line 354 of C:\Users\akulkarni\Desktop\xampp\htdocs\servicecasting\includes\entity.inc).

I have also tried upgrading other modules and core to latest versions as mentioned here http://drupal.org/node/1022736

entity 7.x-1.x-dev (2011-Jan-24), views 7.x-3.x-dev (2011-Jan-22), Drupal core 7.x-dev (2011-Jan-24), profile2 7.x-1.0-beta1, references 7.x-2.x-dev (2011-Jan-14), ctools 7.x-1.0-alpha2

I am not able to figure out what is exactly causing this error?

Edit:

According to http://php.net/manual/en/function.array-flip.php,

array_flip() returns an array in flip order, i.e. keys from trans become values and values from trans become keys.

Note that the values of trans need to be valid keys, i.e. they need to be either integer or string. A warning will be emitted if a value has the wrong type, and the key/value pair in question will not be flipped.

I have done the var_dump($ids); before line 178 in entity.inc ( $passed_ids = !empty($ids) ? array_flip($ids) : FALSE;)

And it looks to me that key/value pair is always in correct format(?).

array
  0 => 
    array
      'nid' => string '6' (length=1)

array
  0 => 
    array
      'uid' => string '1' (length=1)

array
  0 => string '0' (length=1)

array
  0 => 
    array
      'nid' => string '7' (length=1)

array
  0 => 
    array
      'nid' => string '4' (length=1)

array
  0 => 
    array
      'nid' => string '8' (length=1)
like image 383
Ajinkya Kulkarni Avatar asked Jan 25 '11 19:01

Ajinkya Kulkarni


5 Answers

The most common cause of this error is using a something_load() function with an array as argument. This is not supported anymore because the load_multiple() functions need to be used for this now.

Example in D6:

<?php
// Using array with the id was already discouraged in D6 but still worked.
$user = user_load(array('uid' => 1));
$user = user_load(array('name' => 'admin'));
?>

Drupal 7:

<?php
// Argument to a load() function *must* be a single id
$user = user_load(1);

// Querying for another attribute is a bit more complex.
// Note that using reset(user_load_multiple() directly is not E_STRICT compatible.
$users = user_load_multiple(array(), array('name' => 'admin'));
$user = reset($users);
?>

So, the easiest way to catch these is to search for "_load(array".

like image 183
Berdir Avatar answered Nov 13 '22 05:11

Berdir


I ran into the same array_flip error over the weekend, trying to upgrade a custom module to Drupal 7. The problem is that a nested array is getting passed into DrupalDefaultEntityController, but it's expecting a simple array of integers or strings. In my case, I was passing in a nested array in to EntityFieldQuery, when it wants just an array of integers.

To better track down the code that is calling DrupalDefaultEntityController, try inserting the following before line 178 in entity.inc:

drupal_set_message(var_export(debug_backtrace(), TRUE));

... or preferably, install the Devel module and try inserting the following instead:

dpm( debug_backtrace() );
like image 29
Matt V. Avatar answered Nov 13 '22 05:11

Matt V.


The problem comes up when you're using Organic groups field access (Organic Groups 7.x-1.3)

You can usually disable that sub-module unless you do field level access control with OG.

http://drupal.org/node/1102570#comment-5626946

like image 20
Johnathan Elmore Avatar answered Nov 13 '22 07:11

Johnathan Elmore


This can also happen when you call entity_load with an array that is not an array of entity id's as the second argument - see http://api.drupal.org/api/drupal/includes--common.inc/function/entity_load/7 and http://drupal.org/node/1160566 to understand why.

like image 3
Evan Donovan Avatar answered Nov 13 '22 06:11

Evan Donovan


Saw a similar issue in our usage of the latest page_title module. For now, we just disabled the module and it cleaned up the error.

See: http://www.newblood.com/blog/2011/04/26/drupal-7-error-in-page-title-module/

like image 1
Jeremy Avatar answered Nov 13 '22 07:11

Jeremy