Symfony 2 Sonata Media bundle: saving media file image without sonata admin

Tài Nguyễn

I have using the Sonata Admin Bundle and Sonata Media Bundle. It's great, but i have the problem about using Media. https://sonata-project.org/bundles/media/2-2/doc/index.html

Example I have a Post Document, in Post I have $image variable with targetDocument is Media. And When I create new Post, I must upload the image file, and before save Post, I must save the upload image file to Media Entity first and then I pointing the $image of Post to the Media Entity just been saved.

Post Document:

<?php
namespace Acme\Bundle\PostBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @MongoDB\Document(repositoryClass="Acme\Bundle\PostBundle\Repository\PostRepository")
 */
class Post
{
    /**
     * @MongoDB\Id
     */
    protected $id;

    /**
     * @MongoDB\String
     */
    protected $name;

    /**
     * @MongoDB\ReferenceOne(targetDocument="Application\Sonata\MediaBundle\Document\Media", nullable=true )
     */
    protected $image;

Can we have any way to access the Media Entity and save the upload file to it in Post Document function. I had read this but it not help more.

https://sonata-project.org/bundles/media/master/doc/reference/form.html

Tài Nguyễn

The answer is here:

In above code, we must save the upload image to media first and then pointed it to the post:

    $mediaManager = $this->container->get('sonata.media.manager.media');
    $media = new Media();
    $media->setBinaryContent($uploadImage);
    $media->setContext('default');
    $media->setProviderName('sonata.media.provider.image');
    $mediaManager->save($media);
    $post->setImage($media);
    print_r($media);exit;

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Sonata admin bundle preview image from some entity in list mapper without sonata media bundle

From Dev

Sonata Media Bundle with Sonata Admin Bundle 3.0 (or 2.4)

From Dev

API Post to Sonata Media Bundle Symfony 3

From Dev

Symfony 3 - Sonata Aplication Media Bundle error

From Dev

Sonata Media Bundle Override

From Dev

error in sonata media bundle

From Dev

Sonata media validation at admin

From Dev

sonata admin bundle symfony

From Dev

How to validation to sonata media type in symfony 2

From Dev

Sonata admin bundle , Symfony2

From Dev

Sonata Media Bundle Installation error

From Dev

Sonata Admin / Media conflict (CRUDController)

From Dev

Symfony Sonata Media Bundle add images/videos to a user

From Dev

How to override default upload path of sonata media bundle in symfony?

From Dev

How to override default upload path of sonata media bundle in symfony?

From Dev

Customize the Sonata Admin Dashboard - Sonata and Symfony 2

From Dev

Symfony2 Overriding Sonata media Image provider.(Passing interface to constructor)

From Dev

Issue Configuring Sonata Media Bundle with MongoDB

From Dev

Issue Configuring Sonata Media Bundle with MongoDB

From Dev

Image Upload Issue with Symfony3, Doctrine 2 and Sonata Admin Bundle

From Dev

Symfony 2 - Clone entity with one to many sonata media relationship

From Dev

how to override a css of sonata admin bundle in symfony2

From Dev

The show action of Symfony 2 Sonata Admin Bundle is not working

From Dev

Symfony2 and Sonata Admin Bundle - filter timestamp displayed as date

From Dev

Symfony2 and Sonata Admin Bundle - filter timestamp displayed as date

From Dev

Symfony2 Best Practices Editable text sonata admin bundle

From Dev

Include a media type in an Entity from Sonata Admin

From Dev

Batch actions with Sonata Admin Bundle on Symfony

From Dev

Error in file upload with sonata-admin bundle

Related Related

  1. 1

    Sonata admin bundle preview image from some entity in list mapper without sonata media bundle

  2. 2

    Sonata Media Bundle with Sonata Admin Bundle 3.0 (or 2.4)

  3. 3

    API Post to Sonata Media Bundle Symfony 3

  4. 4

    Symfony 3 - Sonata Aplication Media Bundle error

  5. 5

    Sonata Media Bundle Override

  6. 6

    error in sonata media bundle

  7. 7

    Sonata media validation at admin

  8. 8

    sonata admin bundle symfony

  9. 9

    How to validation to sonata media type in symfony 2

  10. 10

    Sonata admin bundle , Symfony2

  11. 11

    Sonata Media Bundle Installation error

  12. 12

    Sonata Admin / Media conflict (CRUDController)

  13. 13

    Symfony Sonata Media Bundle add images/videos to a user

  14. 14

    How to override default upload path of sonata media bundle in symfony?

  15. 15

    How to override default upload path of sonata media bundle in symfony?

  16. 16

    Customize the Sonata Admin Dashboard - Sonata and Symfony 2

  17. 17

    Symfony2 Overriding Sonata media Image provider.(Passing interface to constructor)

  18. 18

    Issue Configuring Sonata Media Bundle with MongoDB

  19. 19

    Issue Configuring Sonata Media Bundle with MongoDB

  20. 20

    Image Upload Issue with Symfony3, Doctrine 2 and Sonata Admin Bundle

  21. 21

    Symfony 2 - Clone entity with one to many sonata media relationship

  22. 22

    how to override a css of sonata admin bundle in symfony2

  23. 23

    The show action of Symfony 2 Sonata Admin Bundle is not working

  24. 24

    Symfony2 and Sonata Admin Bundle - filter timestamp displayed as date

  25. 25

    Symfony2 and Sonata Admin Bundle - filter timestamp displayed as date

  26. 26

    Symfony2 Best Practices Editable text sonata admin bundle

  27. 27

    Include a media type in an Entity from Sonata Admin

  28. 28

    Batch actions with Sonata Admin Bundle on Symfony

  29. 29

    Error in file upload with sonata-admin bundle

HotTag

Archive