1

I have form (AbstractType) that maps to a (doctrine-persisted) entity.

That entity has got a field which is of doctrine's type array.

According to symfony's docs it should be possible to map a form collection field to a PHP's array, but apparently it does not work when the form is mapped to an entity. Symfony expects that field to be a relation, but it is merely an array.

This is not actual code, just an example:

Consider this (doctrine-persisted) entity:

class Article
{
   ...
    /**
     * @ORM\Column(type="array")
     */
    protected $tags;   
   ...
}

And this entity (not managed by doctrine):

class Tag
{
   ...
    public $name;
   ...
}

And this form type:

class ArticleType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('tags', 'collection', array('type' => new TagType()));
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => '\Entity\Article',
        );
    }
}

Tag's form type:

class TagType extends AbstractType
{
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder->add('name', 'text');
    }

    public function getDefaultOptions(array $options)
    {
        return array(
            'data_class' => '\Entity\Tag',
        );
    }
}

When trying to instantiate the ArticleType doctrine will complain something along the lines:

Expected argument of type "\Entity\Tag", "array" given

In file:

vendor/symfony/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php at line 47

Is it even possible to do what I want without writing my own field type?

UPDATE: Maybe I should rephrase my question: How do I tell symfony not to map this field to anything (if I change its name symfony complains that the entity doesn't have such property)?

2 Answers 2

2

I missed one thing. I had fixtures loaded which contained array of associative arrays in the collection property (tags here). I forgot to reload the fixtures after changing them to array of entities (array of Tag instances here).

Now everything works flawlessy, so ineed symfony can map a form collection both to an array and a relation (ArrayCollection). The difference is - I don't have to manually persist the inverse side of the relation.

So this was my bad. Thanks anyway.

Sign up to request clarification or add additional context in comments.

Comments

0

Well as array type will eventually converted to serialized string, mapping it to collection type is inappropriate. Better way should be creating a custom field type where getParent is 'text` and append a data transformer which would transform user input to array or vice versa. Check this cookbook entry.

1 Comment

This is not a viable solution for me. The code above is only an example. In reality each subform is a bit more complicated than a single text field.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.