ReynierPM
ReynierPM

Reputation: 18660

How to deal with array vars in Twig on EasyAdminBundle?

I have this entity definition:

class Operator
{
    ...

    /**
     * @var array
     * @ORM\Column(type="text", nullable=true)
     */
    private $prefix;

    /**
     * @param $prefix
     * @return $this
     */
    public function addPrefix($prefix)
    {
        if (!in_array($prefix, $this->prefix, true)) {
            $this->prefix[] = $prefix;
        }
        return $this;
    }

    /**
     * @param array $prefixes
     * @return $this
     */
    public function setPrefix(array $prefixes)
    {
        $this->prefix = array();

        foreach($prefixes as $prefix) {
            $this->addPrefix($prefix);
        }

        return $this;
    }

    /**
     * @return array The prefixes
     */
    public function getPrefix()
    {
        $prefix = is_array($this->prefix) ? $this->prefix : ['04XX'];
        return array_unique($prefix);
    }

    ...    
}

I am using EasyAdminBundle for manage this entity in the backend so here is the config for it:

easy_admin:
    entities:
        Operator:
            class: PlatformAdminBundle\Entity\Operator
            ...
            form:
                fields:
                    ... 
                    - { property: 'prefix', label: 'prefix' }

Any time I try to create a new Operator I run into this error:

ContextErrorException: Notice: Array to string conversion

I can't find where is the problem since I am using the same on a User entity that inherit from BaseUser (from FOSUser) and it works. This is how it looks like for User entity and should be the same for Operator:

enter image description here

What I am missing? Can any give me some advice? I am stuck!

Upvotes: 0

Views: 216

Answers (1)

Andrzej Piszczek
Andrzej Piszczek

Reputation: 369

Orm prefix column should be array type.

/**
 * @var array
 * @ORM\Column(type="array", nullable=true)
 */
private $prefix;

And run

php app/console doctrine:schema:update --force

Upvotes: 1

Related Questions