1
votes

I installed Sonata Admin and after install Sonata Media

i have class admin for "colors" and set in configureFormFields

->add('image', 'sonata_media_type', array('required' => false,
   'context' => 'default',
   'label' => 'Image',
   'provider'=>'sonata.media.provider.image'
 ))

While stored in the database but when edit show this error

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

Help me, please.

2
Please include the full Exception in your answer and provide the Form and the important parts of your controller.Nicolai Fröhlich

2 Answers

1
votes

You have to set the correct data class:

->add('media', 'sonata_media_type', array(
                     'provider' => 'sonata.media.provider.image',
                     'context'  => 'default',
                     'data_class'   =>  'Application\Sonata\MediaBundle\Entity\Media',
                     'required'   =>  false,
                     'label'    =>  'Image'
                ))

Notice the: 'data_class' => 'Application\Sonata\MediaBundle\Entity\Media',

1
votes

Try setting the data_class option to the correct entity class as the message suggests ...

->add('image', 'sonata_media_type', 
    array(
       'required'    => false,
        'context'    => 'default',
        'data_class' => 'Application\Sonata\MediaBundle\Entity\Media',
        'label'      => 'Image',
            'provider' =>'sonata.media.provider.image'
    )
 )