Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony can't decide which class requires

This is my error:

The form's view data is expected to be an instance of class My\Bundle\Entity\Tags, but is an instance of class Doctrine\Common\Collections\ArrayCollection. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms an instance of class Doctrine\Common\Collections\ArrayCollection to an instance of My\Bundle\Entity\Tags

and this is my form builder

 $builder
            ->add('name')
            ->add('tags','collection',array(
                        'data_class' => 'My\Bundle\Entity\Tags'
                        )
            )
            ->add('save','submit')
        ;

I changed data_class to null ( only that ) and I'm getting error:

The form's view data is expected to be of type scalar, array or an instance of \ArrayAccess, **but is an instance of class My\Bundle\Entity\Tags*. You can avoid this error by setting the "data_class" option to "My\Bundle\Entity\Tags" or by adding a view transformer that transforms an instance of class My\Bundle\Entity\Tags to scalar, array or an instance of \ArrayAccess.

I've tried with a transformer, so it looked like this :

    $transformer = new TagTransformer($this->entityManager);
    $builder
        ->add(
            $builder->create(
                'tags','collection',array(
                    'data_class' => 'My\Bundle\Entity\Tags'
                    )
            )->addModelTransformer($transformer)
        );

and transformer:

public function transform($tag)
{
    if (null === $tag) {
        return "";
    }

    return $tag->toArray();
}

and changed data_class to null again. What I get:

The form's view data is expected to be of type scalar, array or an instance of \ArrayAccess, but is an instance of class My\Bundle\Entity\Tags. You can avoid this error by setting the "data_class" option to "My\Bundle\Entity\Tags" or by adding a view transformer that transforms an instance of class My\Bundle\Entity\Tags to scalar, array or an instance of \ArrayAccess.

When I changed data_class to My\Bundle\Entity\Tags

The form's view data is expected to be an instance of class My\Bundle\Entity\Tags, but is a(n) array. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) array to an instance of My\Bundle\Entity\Tags.

Well.. I mean... wtf? What am I doing wrong? How can I change that?

Edit:

My user entity:

class User
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="name", type="string", length=255)
     */
    private $name;

    /**
     * @ORM\ManyToMany(targetEntity="Tags", cascade={"persist"})
     */
    protected $tags;

// methods, etc..
}
like image 241
mmmm Avatar asked Dec 07 '25 02:12

mmmm


1 Answers

So the reason you're getting errors is because you're using the collection field type a bit incorrect. First of all, the collection field type doesn't support data_class. When you say

->add('tags','collection',array(
    'data_class' => 'My\Bundle\Entity\Tags'
     )
)

you're basically saying that tags (which is an array collection according to your declaration) is actually a tag. If you look at the documentation for the collection type you'll notice that data_class isn't even a supported option. http://symfony.com/doc/current/reference/forms/types/collection.html

so if you want to render a multiple choice list of tags you're looking for the entity type, however these are tags, and if you have any sort of decent site you'll probably have way more than a multiple choice list would be practical for. design wise you want to just have an auto-completer to show what tags already exist with the typed text as you type and then just have the user press enter to add the tag whether is exists or not. then above the auto completer you'd show the tags already added and have and x next to them that they can press on to remove the tag.

you can cheat by just having tags field in your form be a unmapped text type and use javascript to combine the tags together into a string on form submit, then in your action turn the string into your tags.

like image 171
Derick F Avatar answered Dec 08 '25 15:12

Derick F