Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I have a content type that has a multi-value taxonomy field. When I create a new item and select several values, I am presented with the following error:

Array to string conversion in DrupalDefaultEntityController->cacheGet() (line 369 of /path/to/drupal/installation/includes/entity.inc).

The values are saved and everything appears to be working, but having this error appear every time is a little distracting, especially for the office staff who are actually entering the data.

Why does this happen?

share|improve this question
 
Seems to be a core bug - drupal.org/node/1525176 –  Mathankumar Apr 3 at 5:30
4  
This question appears to be off-topic because it is about core bug already posted in issue queue and apparently it is not a question that appeared during bug fixing. See this meta thread. –  Mołot Aug 23 at 13:10

1 Answer

Problem:

Since PHP 5.4 this notice appears when an array is compared as the string "array". The problem is in the file includes/entity.inc in the method cacheGet which gathers items from a static cache.

On line 369 it uses array_assoc_diff which compares the array indexes and values and returns the difference of the two arrays. Unfortunately it doesn't do this recursively meaning if it is a multidimensional array it will use "array" as a string and compare it with the other array. This is what causes the notice to occur.

It seems that the code is flawed and not performing the proper function get a given item from the cache.

First Solution:

The first solution I came up with to resolve this issue was to loop through each item of the condition and use the key of the conditions to check if the entity value for that key isset. If it it isn't set then there is a difference. This would mean that the entities with that key would be unset. I don't like using nested foreach loops though this came to me as the most immediate solution to resolve the problem.

The fix was to replace lines 369 to 371:

if (array_diff_assoc($conditions, $entity_values)) {
  unset($entities[$entity->{$this->idKey}]);
}

With this piece of code:

foreach ($conditions as $key=>$values) {
    if (!isset($entities[$key])) {
        unset($entities[$entity->{$this->idKey}]);
    }
    elseif ($conditions[$key] !== $entity_values[$key])
    {
        unset($entities[$entity->{$this->idKey}]);
    }
}

Second Solution:

A second thought I've had is to add a new function that has a similar functionality as the php function array_diff_assoc except that it does the comparison recursively. While looking through the on-line PHP manual I found a function that would accomplish that.

Place this at top of entity.inc after the opening php tags:

function array_diff_assoc_recursive($array1, $array2) {
    $difference=array();
    foreach($array1 as $key => $value) {
        if( is_array($value) ) {
            if( !isset($array2[$key]) || !is_array($array2[$key]) ) {
                $difference[$key] = $value;
            } else {
                $new_diff = array_diff_assoc_recursive($value, $array2[$key]);
                if( !empty($new_diff) )
                    $difference[$key] = $new_diff;
            }
        } else if( !array_key_exists($key,$array2) || $array2[$key] !== $value )     {
            $difference[$key] = $value;
        }
    }
    return $difference;
}

Replace what you see on line 369 from:

if (array_diff_assoc($entity_values, $conditions)) {

With this code:

if (array_diff_assoc_recursive($entity_values, $conditions)) {
share|improve this answer
2  
This is a known issue, see drupal.org/node/1525176. The issue needs a test (easy, just an example call that triggers this notice) to be able to be committed. –  Berdir Jul 17 at 7:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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