2
votes

My question is related to: How to configure m2m relationship in Sonata Admin?

After configure admin class according to related answer I got:

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

So I added this property:

->add('types', 'collection', array(
            'type'         => 'text',
            'allow_add'    => true,
            'data_class' => 'Syloc\Bundle\GooglePlacesBundle\Entity\PlaceType',
        ));

This leads to error:

The form's view data is expected to be an instance of class Syloc\Bundle\GooglePlacesBundle\Entity\PlaceType, but is an instance of class Doctrine\ORM\PersistentCollection. 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\ORM\PersistentCollection to an instance of Syloc\Bundle\GooglePlacesBundle\Entity\PlaceType.

3

3 Answers

2
votes

I ended with:

     ->add('types', 'entity', array(
            'class'         => 'GooglePlacesBundle:PlaceType',
            'property' => 'type',
            'multiple' => true,
        ));
0
votes

The data_class attribute is not an option on collection. It seems like you might want want your 'type' to be:

->add('types', 'collection', array(
    'type'      => '\Syloc\Bundle\GooglePlacesBundle\Entity\PlaceType',
    'allow_add' = true
));

Now this is assuming that PlaceType is a custom form type you created and not just your entity.

See my answer here if this is what you looking for, for further explanation: How do I make a form with a bunch of unrelated entities in Symfony2?

0
votes

try to use the sonata-type-collection :

->add('types', 'sonata_type_collection', array(
                'by_reference' => true,
                'btn_add' => false,
                'required' => true,
                'label' => false,
                'type_options' => array('delete' => false),
                'cascade_validation' => true,
                ), array(
                'edit' => 'inline',
                'inline' => 'table',
            ))