MadManMonty
MadManMonty

Reputation: 816

Illegal offset type in isset or empty in EntityChoiceList.php line 273

Within my Symfony2 project I've attempted to dynamically generate the entities used within my form type, by-passing the use of query builder etc.

To he entity choices property I am supplying an array of entities to be used. On page load everything seems fine and the correct content is displayed. However on form submission I get

Illegal offset type in isset or empty in EntityChoiceList.php line 273

at ErrorHandler ->handle ('2', 'Illegal offset type in isset or empty',
'..../Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php', '273', array('key'     => object(myEntity))) in  ..../Symfony/Bridge/Doctrine/Form/ChoiceList/EntityChoiceList.php at line 273   
            .....
            return isset($entities[$key]) ? $entities[$key] : null; 
            .....

What has me stumped is if I add var_dump(isset($this->entities[$key]));exit; above this line I am returned 'bool(true)' which to me means the key does exist.

As background I have attempted to extend the EntityType, for ease within my project and added:

public function getDefaultOptions(array $options)
{   
    $defaultOptions = array(
        'em'                => null,
        'class'             => 'Acme\TestBundle\Entity\myEntity',
        'property'          => null,
        'query_builder'     => null,
        'choices'           => $this->myEntityArray,
    );

    $options = array_replace($defaultOptions, $options);
    $defaults = parent::getDefaultOptions($options);        
    return $defaults;
}    

Has any one any ideas why I getting this error, or am I going about my issue all wrong anyway, with trying to pass an array of entities to choices?

Upvotes: 12

Views: 8596

Answers (2)

Jonathan
Jonathan

Reputation: 3024

If you're getting this while trying to remove an element from an ArrayCollection it's probably because you've typed:

$list->remove($item) instead of $list->removeElement($item)

Upvotes: 55

Willlem-Jan
Willlem-Jan

Reputation: 34

I'm guessing you already solved this some other way, and this isn't a real answer either.

But I'm guessing either $entities isn't an array on that point, or $key isn't a scalar value. For debugging you should use:

<?php
if (!is_array($entities) || !is_scalar($key)) {
    var_dump($key, $entities));exit;
}

How you now tested this, it would stop on the first pass in that function. Symfony Forms use quit a lot of recursion, so an exit in any function usually doesn't help you.

Upvotes: -1

Related Questions