简体   繁体   中英

You can avoid this error by setting the “data_class” when use sonata_media_type

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.

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'
    )
 )

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',

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM