[英]Sonata Media Bundle - How to get Image or Gallery by Id
Is it possible to get an image or gallery from Sonata Media Bundle by Id? 是否可以从Id的Sonata Media Bundle获取图像或画廊?
I know how to get an image or gallery which is related to an entity. 我知道如何获取与实体相关的图像或画廊。 But in this case I just want to get an image or gallery without an entity.
但是在这种情况下,我只想获取没有实体的图像或画廊。
Something like: 就像是:
$media = $mediaProvider->getMediaById(12);
or 要么
$gallery = $mediaProvider->getGalleryById(2);
Does anyone know how to do this? 有谁知道如何做到这一点?
I found a solution! 我找到了解决方案!
In the controller get the gallery: 在控制器中获取图库:
$galleryId = 3;
$repo = $this->getDoctrine()->getRepository('ApplicationSonataMediaBundle:Gallery');
$gallery = $repo->find($galleryId);
$gallery = $gallery->getGalleryHasMedias();
And in the twig template: 在树枝模板中:
{% for img in gallery %}
{% media img.media, 'gallery' %}
{% endfor %}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.