array_flip():只能翻转字符串和整数值!in DrupalDefaultEntityController->load()

2022-08-30 12:12:21

我最近将我的模块迁移到Drupal7(在PHP版本5.3.1上),现在我收到以下错误:

    * 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).

我还尝试将其他模块和核心升级到最新版本,如这里所述 http://drupal.org/node/1022736

实体 7.x-1.x-dev (2011 年 1 月 24 日), views 7.x-3.x-dev (2011-Jan-22), Drupal core 7.x-dev (2011-Jan-24), profile2 7.x-1.0-beta1, 参考文献 7.x-2.x-dev (2011-Jan-14), ctools 7.x-1.0-alpha2

我无法弄清楚究竟是什么导致了此错误?

编辑:

根据 http://php.net/manual/en/function.array-flip.php

array_flip() 按翻转顺序返回数组,即来自 trans 的键成为值,来自 trans 的值变为键。

请注意,trans 的值必须是有效的键,即它们必须是整数或字符串。如果值的类型错误,则会发出警告,并且有问题的键/值对不会被翻转。

我已经在entity.inc(var_dump($ids);$passed_ids = !empty($ids) ? array_flip($ids) : FALSE;)

在我看来,键/值对总是采用正确的格式(?)。

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)

答案 1

此错误的最常见原因是使用带有数组作为参数的 something_load() 函数。这不再受支持,因为现在需要使用load_multiple()函数。

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);
?>

因此,捕获这些内容的最简单方法是搜索“_load(数组) 。


答案 2

我在周末遇到了同样的array_flip错误,试图将自定义模块升级到Drupal 7。问题在于嵌套数组被传递到DrupalDefaultEntityController中,但它需要一个简单的整数或字符串数组。在我的情况下,我正在将嵌套数组传入EntityFieldQuery,当它只需要一个整数数组时。

为了更好地跟踪调用 DrupalDefaultEntityController 的代码,请尝试在 entity.inc 中的第 178 行之前插入以下内容:

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

...或者最好是安装Devel模块并尝试插入以下内容:

dpm( debug_backtrace() );