尝试在 symfony 2 中显示单个数据库中的多条记录

发布于 2024-11-30 15:51:38 字数 1174 浏览 1 评论 0原文

在我正在进行的项目中,我需要在网站上显示 5 篇最新的新闻文章。在控制器中,我编写了以下代码:

      $news = $repository->createQueryBuilder('p')
          ->Where('p.contenttype = :type')
          ->setParameter('type', 'newsarticle')
          ->orderBy('p.lastedit', 'ASC')
          ->getQuery();

      $latestnews = $news->getResult();

由于某种原因,这不起作用,因为我收到错误消息:

第 34 行的“ShoutMainBundle:Default:page.html.twig”中不存在“Array”的项目“url”

但是,当我将 getResult(); 更改为 getSingleResult() ; 它可以工作,但只会显示一条记录(这是我使用该代码时所期望的)。

这就是我对自己应该做什么感到困惑和困惑的地方。我用谷歌搜索了“如何在 symfony 中显示多个记录”,但没有找到答案。 (如果答案已经存在,我提前为此道歉)。在普通的 PHP 中,我希望执行一个 foreach 循环(无论如何类似的东西)来获得我需要的结果。但我也有一种感觉,要实现我想要的目标,我需要在 Twig 中做一些事情。但我需要做什么我不知道。

任何对此的帮助将不胜感激。

谢谢

编辑: 以下是用于显示此内容的模板代码:

            <section id="latestnews">
            <h2>Latest News</h2>
            <ul>
                <li><a href="..{{ news.url }}" title="Read {{ news.title }}" />{{ news.title }}</a></li>
            </ul>
        </section>

In the project I'm working on, I need to display 5 of the latest news articles on the website. In the Controller, I have written the following code:

      $news = $repository->createQueryBuilder('p')
          ->Where('p.contenttype = :type')
          ->setParameter('type', 'newsarticle')
          ->orderBy('p.lastedit', 'ASC')
          ->getQuery();

      $latestnews = $news->getResult();

This doesn't work for some reason, as I get the error message:

Item "url" for "Array" does not exist in "ShoutMainBundle:Default:page.html.twig" at line 34

However, when I change the getResult(); to getSingleResult(); it works, but will only display the one record (which is what I expect when I use that code).

This is where I come unstuck and confused about what I'm supposed to be doing. I have googled "how to display multiple records in symfony" and I haven't found the answer. (If the answer has been out there, I apologise in advance for this). In normal PHP, I would expect to do a foreach loop (something similar anyway) to get the results which I need. But I also have a feeling that to achieve what I want I need to do something in Twig. But what I need to do I don't know.

Any help with this would be very much appreciated.

Thanks

Edit:
Here is the template code that is used to display this:

            <section id="latestnews">
            <h2>Latest News</h2>
            <ul>
                <li><a href="..{{ news.url }}" title="Read {{ news.title }}" />{{ news.title }}</a></li>
            </ul>
        </section>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

红ご颜醉 2024-12-07 15:51:38

您的代码尝试从变量 news 中读取,并假设该变量具有字段 urltitle。如果您的控制器返回一个数组,您必须将 news 作为数组并对其进行迭代。

<section id="latestnews">
  <h2>Latest News</h2>
  <ul>
  {% for news in latestnews %}
   <li><a href="..{{ news.url }}" title="Read {{ news.title }}" />{{ news.title }}</a></li>
  {% endfor %}
  </ul>
</section>

Your code tries to read from the variable news, and assumes that this variable has fields url and title. If your controller returns an array, you have to tread news as an array and iterate over it.

<section id="latestnews">
  <h2>Latest News</h2>
  <ul>
  {% for news in latestnews %}
   <li><a href="..{{ news.url }}" title="Read {{ news.title }}" />{{ news.title }}</a></li>
  {% endfor %}
  </ul>
</section>
泪眸﹌ 2024-12-07 15:51:38

看起来在您的模板中,您正在寻找未找到的对象。它正在数组对象中查找 url,但它不存在。我认为您需要进行检查,看看数组中是否存在,如果存在,则回显。所以类似 if(news.url) echo news.url;

显然,这可能不是那么精确的语法,我对 twig 不太熟悉,但与此类似。

It looks like in your template, you're looking for an object that's not found. It's looking for the url in an array object, but it doesn't exist. I think you need to put in a check, to see if that exists in the array, and then echo if it does. So something like if(news.url) echo news.url;

It may not be that exact syntax obviously, I'm not all that familiar with twig, but something similar to that.

℉服软 2024-12-07 15:51:38

您需要循环遍历 Twig 中的“新闻”结果数组。

{% for n in news %}

    <li><a href="..{{ n.url }}" title="Read {{ n.title }}" />{{ n.title }}</a></li>

{% endfor %}

You need to loop through the "news" results array in Twig.

{% for n in news %}

    <li><a href="..{{ n.url }}" title="Read {{ n.title }}" />{{ n.title }}</a></li>

{% endfor %}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文