-->

Sonata Media: The file could not be found

2019-08-09 01:22发布

问题:

I'm trying to attach an image to an Entity but when I submit the form, an error is displayed saying: The file could not be found.

Here is my code to build the form:

$builder
    ->add('email', 'email', array('label' => 'Email'))
    ->add('name', 'text', array('label' => 'Name'))
    ->add('image', 'sonata_media_type', array('label' => 'Image', 'provider' => 'sonata.media.provider.image', 'context'  => 'default'))
;

What am I doing wrong?

Thanks

回答1:

First, you need to 'link' your Entity to the SonataMediaBundle :

/**
 * @ORM\ManyToOne(targetEntity="Application\Sonata\MediaBundle\Entity\Media")
 * @ORM\JoinColumn(name="image", referencedColumnName="id")
 */
private $image;

And then in your Admin, in the configureFormFields(FormMapper $formMapper) :

->add('image', 'sonata_type_model_list', array('label' => 'Image', 'required' => false)))

In my projects, I've made it like that :)