Search code examples
phpsymfonysonata-adminsymfony-sonatasonata-media-bundle

Show attached image after upload trough SonataAdmin


I have a Product entity mapped as follow:

class Product
{

    use IdentifiedAutogeneratedEntityTrait;
    use BaseEntityTrait;

    ......

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

   ......

    public function setImage(\Application\Sonata\MediaBundle\Entity\Media $image)
    {
        $this->image = $image;
    }
}

Then in my admin form I have this:

protected function configureFormFields(FormMapper $form)
{
    $form
            ->add('product_name', null)
            ->add('product_description', null)
            ->add('image', 'sonata_type_admin');
}

When I add a new product I can upload a image and yes it works but I can see the image on the page where I'm creating the product, how I can do that?


Solution

  • I think you have to use sonata_type_model_list and not sonata_type_admin.

    protected function configureFormFields(FormMapper $form)
    {
        $form
                ->add('product_name', null)
                ->add('product_description', null)
                ->add('image', 'sonata_type_model_list');
    }