php array_flip():只能翻转 STRING 和 INTEGER 值!在 DrupalDefaultEntityController->load()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4798047/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 14:25:13  来源:igfitidea点击:

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

arraysdrupaldrupal-modulesphp

提问by Ajinkya Kulkarni

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

我最近将我的模块迁移到 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).

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

我还尝试将其他模块和核心升级到最新版本,如这里提到的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

实体 7.x-1.x-dev (2011-Jan-24),视图 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

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,

根据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.

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

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

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

我已经var_dump($ids);在 entity.inc ( $passed_ids = !empty($ids) ? array_flip($ids) : FALSE;) 中完成了之前的第 178 行

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)

回答by Berdir

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.

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

Example in D6:

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:

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".

因此,捕获这些的最简单方法是搜索“_load(array”)。

回答by Matt V.

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.

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

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

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

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

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

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

dpm( debug_backtrace() );

回答by Johnathan Elmore

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

当您使用有机组字段访问(有机组 7.x-1.3)时会出现问题

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

您通常可以禁用该子模块,除非您使用 OG 进行字段级访问控制。

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

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

回答by Evan Donovan

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/7and http://drupal.org/node/1160566to understand why.

当您使用不是实体 ID 数组的数组作为第二个参数调用 entity_load 时,也会发生这种情况 - 请参阅http://api.drupal.org/api/drupal/includes--common.inc/function/entity_load/ 7http://drupal.org/node/1160566了解原因。

回答by Jeremy

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.

在我们使用最新的 page_title 模块时看到了类似的问题。现在,我们只是禁用了该模块并清除了错误。

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

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

回答by marcvangend

Are you using Insert Module? See http://drupal.org/node/850946.

你在使用插入模块吗?请参阅http://drupal.org/node/850946

When it comes to specific errors like this, I think you're better off searching the issue queue at drupal.orgthan asking on SO.

当涉及到这样的特定错误时,我认为您最好在drupal.org上搜索问题队列而不是在 SO 上询问。

回答by warmth

Example of good use:

善用范例:

<?php
$user=user_load(arg(1));
$username=$user->name;
print strtolower(preg_replace('/[^a-zA-Z0-9\-]/si' , '-' , $username));
?>

回答by kenorb

This could be problem of the bad coding (e.g. loading invalid entities or running some old Drupal 6 code in Drupal 7):

这可能是错误编码的问题(例如加载无效实体或在 Drupal 7 中运行一些旧的 Drupal 6 代码):

  • If you've any custom modules, double check your code for common mistake
  • If you're using contrib module, find the right bug against it and apply the patch (if available) or raise the new one.
  • 如果您有任何自定义模块,请仔细检查您的代码是否存在常见错误
  • 如果您正在使用 contrib 模块,请找到针对它的正确错误并应用补丁(如果可用)或提出新补丁。

Troubleshooting:

故障排除:

  • You may try to dump the backtrace by calling and printing print_r(debug_backtrace()),
  • Use your scm tool to get back to the point where it was working fine, and compare the changes, so eventually you'll find where the problem is (e.g. gitk, git log --patch, etc.)
  • please use Coder Reviewmodule to find bugs in your code after the update (e.g. drush --contrib --no-empty --upgrade7x coder-review).
  • Install XDebugto track your issue by trace log or step-by-step debugging.
  • 您可以尝试通过调用和打印来转储回溯print_r(debug_backtrace())
  • 用你的供应链管理工具得到回到它工作正常了点,和比较的变化,所以最终你会发现问题的所在(例如gitkgit log --patch等)
  • 请使用Coder Review模块在更新后查找代码中的错误(例如drush --contrib --no-empty --upgrade7x coder-review)。
  • 安装XDebug以通过跟踪日志或逐步调试来跟踪您的问题。


Alternatively you may debug your code by defining the following temporary hook:

或者,您可以通过定义以下临时挂钩来调试代码:

/**
 * Implements hook_watchdog().
 */
function foo_watchdog($log_entry) {
  if ($log_entry['type'] == 'php' && $log_entry['severity'] <= WATCHDOG_WARNING) {
    // Old school
    var_dump(debug_backtrace()); // Optionally add: exit();

    // Devel: Log the backtrace into temporary file: drupal_debug.txt
    // Locate via: $ drush eval "echo file_directory_temp() . '/drupal_debug.txt'"
    function_exists('dd') && dd(debug_backtrace());
  }
}

Clear the cache before testing it.

在测试之前清除缓存。

It'll print the backtrace on every PHP warning or error with arguments, so you can find your invalid parameters which were passed into the Drupal core.

它会在每个带有参数的 PHP 警告或错误上打印回溯,因此您可以找到传递给 Drupal 核心的无效参数。

回答by Enno

I've also gotten this message when erroneously trying to load multiple nodes via node_load_multiple($nids) where $nids was not an array of node-ids.

当我错误地尝试通过 node_load_multiple($nids) 加载多个节点时,我也收到了此消息,其中 $nids 不是节点 ID 数组。

For example, using the result of an EntityFieldQuery, and calling node_load_multiple($result['node']) instead of?node_load_multiple(array_keys($result['node'])).

例如,使用 EntityFieldQuery 的结果,并调用 node_load_multiple($result['node']) 而不是?node_load_multiple(array_keys($result['node']))。

回答by Shilpa

Thanks for the post it worked for me!, I was encountering with this problem in Drupal 7 since long time and could fighure out the problem. Bottom line

感谢这篇对我有用的帖子!,我在 Drupal 7 中遇到这个问题很长时间了,并且可以解决这个问题。底线

"Do not pass array values to array_flip for any kind of entities, eg: If you are trying to load and entity user_load() or field_collection_item_load() for loading field collection items, Pass only values in the string rather than array itself."

“对于任何类型的实体,不要将数组值传递给 array_flip,例如:如果您尝试加载实体 user_load() 或 field_collection_item_load() 以加载字段集合项,请仅传递字符串中的值而不是数组本身。”

Thanks!!

谢谢!!