Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sonata_media_type widget issue #974

Closed
fbxcelsior opened this issue Mar 14, 2016 · 9 comments
Closed

sonata_media_type widget issue #974

fbxcelsior opened this issue Mar 14, 2016 · 9 comments

Comments

@fbxcelsior
Copy link

Hi,

I encountered a strange issue. I have an entity that has a Media object relation and in the admin form for that entity I am using a sonata_media_type form element to render the data field. If I select a new image and update the parent entity, it works as expected, i.e, a new Media object is created and persisted, but then, if I replace that image with a new one, a new Media object is created (as expected) but the old Media object's image is also updated to the new image that was just uploaded, therefore ending up with two Media objects that look the same (although with different names).

Have any of you came across this problem before?

Symfony 2.8, with the master versions of Sonata Admin and Sonata Media.

Thank you in advance.

Best regards,

Filipe

@Flo22100
Copy link

I got the same problem, do you find a solution to this?

@OskarStark
Copy link
Member

@ahmetakbn i think you have the same problem, am I right?

@codebach
Copy link
Contributor

codebach commented Aug 17, 2016

@OskarStark yes, the issue I was talking about is exactly this.

@Flo22100
Copy link

Flo22100 commented Aug 18, 2016

I hotfixed it by extending ImageProvider with this code:

namespace AppBundle\Provider;

use Sonata\MediaBundle\Model\MediaInterface;
use Sonata\MediaBundle\Provider\ImageProvider as SonataImageProvider;

class ImageProvider extends SonataImageProvider
{

    protected function doTransform(MediaInterface $media)
    {
        // Check if the entity is new
        if (!is_null($media->getId())) {
            // Prevent the file from being erased
            $media->resetBinaryContent();
            // Prevent the provider reference from being erased
            $media->setProviderReference($media->getPreviousProviderReference());
            return;
        } else {
            // If new media, nothing special to do
            return parent::doTransform($media);
        }
    }
}

And adding this in parameters:

sonata.media.provider.image.class: AppBundle\Provider\ImageProvider

It works in my use case but it is sure to fail with new_on_update param set to false on the form.

@InnerFlameFact
Copy link

Hi all. I got the same problem.
Any news about fix?

Thanks you!

@anacona16
Copy link

anacona16 commented Mar 31, 2017

I have the same problem!

sonata-project/media-bundle 3.5.0
symfony 3.2.6

@core23
Copy link
Member

core23 commented Mar 31, 2017

Can you provide a PR with a fix @storke ?

@Flo22100
Copy link

@core23 Has I said, this fix works just for my use case and breaks the new_on_update option.

I didn't find a better solution so I will not PR it, maybe it could inspire someone to correct it?

@jordisala1991
Copy link
Member

Close because #1273 is just the same issue with a better explanation and more comments.

If this issue is completely different ping me to reopen, otherwise, lets centralize discussions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants