Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to transform value for property path "tagname". Expected a Doctrine\Common\Collections\Collection object

I am working with two ManyToMany related entities, namely category and tag.

The entity Tag(relevant details):

/**
 * 
 * @var string
 *
 * @ORM\Column(name="tagname", type="string")
 */
protected $tagname;

/**
 * @ORM\ManyToMany(targetEntity="Category", mappedBy="tags")
 */
protected $categories; 

The entity Category(relevant details):

/**
 * 
 * @var string
 *
 * @ORM\Column(name="CategoryName", type="string",length=200)
 */
protected $categoryname;

/**
 * @ORM\ManyToMany(targetEntity="Tag", inversedBy="categories")
 */
protected $tags;

I have a form with a select-input(CategoryType) and a multiple select-input(TagType) fields. Both the fields are EntityType fields. The TagType is embedded inside the CatgoryType.

For this I am not able to utilise the cascade=persist functionality and am adding the submitted tags manually inside my controller. On submission the form data gets persisted in the database without any issues.

The problem is, after submission, when I fetch the submitted category(and the associated tags) in my controller, and pass it to the form, I get this error - Unable to transform value for property path "tagname": Expected a Doctrine\Common\Collections\Collection object.

The var_dump result of the fetched category object(var_dump($category->getTags()->getValues());) gives me an array of the associated Tag objects, with the property protected 'tagname' => string 'tag1'.

From what I understand Interface Collection is quite similar to a php array and My guess is that the tagname field expects all the tagnames in an ArrayCollection or Collection object format. I am not sure whether what is the specific difference.

However I am still clueless how do I pass the already persisted category object in my form.

Here are the categoryname and tags field in the CategoryType:

    $builder->add('categoryname', EntityType::class, array(
        'class' => 'AppBundle:Category',
        'query_builder' => function(EntityRepository $er) {
        return $er->createQueryBuilder('c')
            ->orderBy('c.id', 'ASC');
},
        'choice_label' => 'categoryname',
        'expanded' => false,
        'multiple' => false,
        'label' => 'Choose Category',
    ));
    $builder->add('tags', CollectionType::class, array(
        'entry_type' => TagType::class,
        'allow_add' => true,
        'by_reference' => false,
        'allow_delete' => true,
    )); 

Here is the embedded tagname field in the TagType:

    $builder->add('tagname', EntityType::class, array(
        'class' => 'AppBundle:Tag',
        'query_builder' => function(EntityRepository $er) {
    return $er->createQueryBuilder('t')
                    ->orderBy('t.id', 'ASC');
},
        'choice_label' => 'tagname',
        'expanded' => false,
        'multiple' => true,
        'label' => 'Choose Tags',
    ));

Any ideas?

like image 998
utkarsh2k2 Avatar asked Oct 29 '22 23:10

utkarsh2k2


1 Answers

Try to rid off 'multiple' => true in embedded form. This worked for me.

like image 142
Sruj Avatar answered Jan 04 '23 13:01

Sruj