【问题标题】:Key "catTitle" for array with keys "0" does not exist in EagleShopBundle:global:product.html.twigEagleShopBundle:global:product.html.twig 中不存在键为“0”的数组的键“catTitle”
【发布时间】:2016-01-28 07:31:33
【问题描述】:

我正在尝试加入两个表并在 twig 模板中打印一个值,但我遇到了这个问题。

这是我的控制器操作。

/**
     * @Route("products/display/{id}")
     * @Template()
     */
    public function displayAction($id) {

        $em = $this->container->get('doctrine.orm.entity_manager');
        $qb = $em->createQueryBuilder();

        $qb->select('p', 'pc.catTitle')
                ->from('EagleShopBundle:Products', 'p')
                ->leftJoin('EagleShopBundle:ProductCategory', 'pc', \Doctrine\ORM\Query\Expr\Join::WITH, 'pc.id = p.category')
                ->where($qb->expr()->eq('p.id', '?5'))
                ->setParameter(5, $id);

        $product = $qb->getQuery()->getResult();

        return $this->render("EagleShopBundle:global:product.html.twig", array(
                    'product' => $product,
                    'image_path' => '/bundles/eagleshop/images/'
        ));
    }

这是我与问题相关的树枝文件行,

<small class="pr_type">{{product.catTitle}}</small>

但是我遇到了这个问题,而不是打印“catTitle”,

键为“0”的数组的键“catTitle”不存在 EagleShopBundle:global:product.html.twig

【问题讨论】:

    标签: symfony doctrine twig


    【解决方案1】:

    很简单:getResult() 返回一个对象数组,即使只有一个。如果您希望此查询只返回一个对象,请使用getOneOrNullResult()。但在那之后,在显示结果之前,您需要检查是否返回 smth(例如:product instanceof Products)或返回 null。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-14
      • 2023-03-23
      • 2013-11-11
      • 1970-01-01
      • 1970-01-01
      • 2013-08-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多