繁体   English   中英

从树枝文件中的数据库渲染图像

[英]render image from database in twig file

我有一个带有列路径和alt的表MEDIA,表POST链接到表介质。 因此,如果用户添加新帖子,则新帖子的图像ID链接到表格媒体。 我想在我的页面中渲染帖子的图片,但出现此错误:

下面的屏幕快照显示了表之间的链接: 在此处输入图片说明

在第18行的src \\ FLY \\ BookingsBundle \\ Resources \\ views \\ Post \\ show.html.twig中的模板渲染过程中引发了异常(“ Notice:Undefined index:post”)。

这就是我所做的:

    {% for post in post %}
    <figure class="col-xs-3 col-sm-2">
    <span><img alt="airline" width="300" height="120" src="{{ post.image.path }}" ></span>
</figure>
{% endfor %}

这是我的PostController.php

   public function showAction($id)
    {
        $em = $this->getDoctrine()->getManager();
        $user = $this->getUser();
        $findEntity = $em->getRepository('FLYBookingsBundle:Post')->findBy(array('user' => $user ));
        $entity  = $this->get('knp_paginator')->paginate($findEntity,$this->get('request')->query->get('page', 1),9
        );

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Post entity.');
        }

        $deleteForm = $this->createDeleteForm($id);

        return array(
            'entity'      => $entity,
            'delete_form' => $deleteForm->createView(),
        );
    }

您在PostController上使用$entity ,但在树枝中尝试使用不存在的post

另外,您正在对该变量进行foreach ,但是您应该只访问它,因为它可能只是一个对象或null而不是数组

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM