Searching for a link between mediafiles and products

Hi there, I am building a plugin to find all faulty, better said missin, mediafiles and list them. I have it pretty much going, using the media repository.
I also want to add all the products that refer to the (missing) image or media file. As an example, in dockware (locally running shopware 6), there is an example Product called „Main product with advanced prices“ and its pruduct number is „SWDEMO10002“. I changed the cover pic of this item to one of the existing pictures, but I am not able to find a link between them. There must be something to start from the ID of a media file and route all the way to this product number. But I cant find it.

Can anyone help?

TLDR: I need something to be able to find all the product that use a specific image / media file

Hi hoyohoo,
I’m no developer, so maybe I understood you wrong. There is an introduction to working with Media and a custom field on the product details page: https://developer.shopware.com/docs/guides/plugins/plugins/storefront/use-media-thumbnails. Maybe you will find an approach to the solution in this chapter.

The connection you are looking for is probably the product_media table.

Kind regards,
Markus

Hi there, thanks mate. I was not really looking for the information in the given link, but I found what I was looking for with your last sentence. I transeferthe product_media.repository in my services.xml as an argument to my controller and could use it. It has all the links between the media files and the products I needed.


For anyone, who might look into this in the future: The products in the pruduct.repository all have the property „coverId“ when a cover is assigned. The coverId however is only to be found in the product_media.repository as the IDs of the elements inside here. Lets say your product hat the Id „123“ and your cover pics Id is „456“. The coverId of your product however will be something completely different, lets say „abc“.
So, in product_media.repository you can find the element named „abc“ with the unique_id „abc“ and it will have the properties „mediaId“, which in this case is „456“ and productId, here „123“. With this connection, you can get all the Ids you need and therefore use them to get all the information and properties you need.