Invalid Argument Supplied for Foreach in Mage/Eav/Model/Entity/Collection/Abstract.php – Magento 1.8 Fix

ce-1.8.1.0eaverrormagento-1.8upgrade

After upgrading magento to 1.8.1.0 (from 1.7.) I am now seeing this error in my system.log:

2014-05-26T10:39:56+00:00 ERR (3): Warning: Invalid argument supplied for foreach() in app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php on line 776

Line 776 of Abstract.php:

foreach ($fields as $alias=>$field) {
    if (isset($this->_joinFields[$alias])) {
        throw Mage::exception(
            'Mage_Eav',
            Mage::helper('eav')->__('A joint field with this alias (%s) is already declared', $alias)
        );
    }
    $this->_joinFields[$alias] = array(
        'table' => $tableAlias,
        'field' => $field,
    );
}

Anybody else seeing this error and is it a bad one or can it be ignored?

Best Answer

I had the problem too and your comment hinted me to the right spot.

As you said the error comes from the public function joinTable() in app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php.

Unlike you I have used joinTable() in a Module of mine.

The head of that function says the following

 * @param string|array $table
 * @param string $bind
 * @param string|array $fields
 * @param null|array $cond
 * @param string $joinType
 * @return Mage_Eav_Model_Entity_Collection_Abstract
 */
public function joinTable($table, $bind, $fields = null, $cond = null, $joinType = 'inner')

Problem here is that $fields should not be of type string as said in the @param because it is used in a foreach loop.

I modified my call of joinTable() to not give a string, but an array with a string. No log entries so far.

My guess for you would be that some module uses joinTable and passes just a string for $fields. Are you able to debug into your code? That way you would have the stacktrace and could determine which modules fault that was.

EDIT:

I've looked further into it to anser your last questions:

"is it a bad one"

No. The join will still work, because later on, deep in call structure reachable from line 817 of Abstract.php there is a check if $fields (until then called $cols) is an array (/lib/Varien/Db/Select.php line 411) and if not will be converted into one for further processing (this is missing in Abstract.php).

"can it be ignored"

I'd say yes. It crams your log, but not disabling your project (at this point).