用树枝展示教义集? (Symfony2)

发布于 2024-12-18 17:47:41 字数 846 浏览 0 评论 0原文

我有一个非常简单的代表新闻的学说实体;这个新闻可以与很多图片联系起来,所以我决定使用教义集。问题是,我想检索这些图片并将它们显示在我的模板中......但它似乎不起作用。你知道我该怎么做吗?

这是我尝试过的:

{% for annonce in annonces %}
    <div class="annonce_item">
    {% for photo in annonce.photo  %}
        <img src="{{ photo.path }}" alt="" />
    {% endfor %}
</div>
<!-- End .annonce_item -->
{% endfor %}

annonce 是新闻类,photo 是集合:

/**
 * @ORM\OneToMany(targetEntity="Photo", mappedBy="id",cascade={"persist"})
 */

private $photo;

当我尝试在浏览器中显示此页面时,出现以下异常:

渲染模板期间抛出异常(“注意:未定义索引:>id in >/Applications/MAMP/htdocs/ApacheImmobilier/vendor/doctrine/lib/Doctrine/ORM/Persisters/Basi>” cEntityPersister.php 第 1274 行”)位于“APPagesBundle:Index:index.html.twig”第 45 行。

谢谢!

I have a quite simple doctrine entity that represent a news ; this news can be linked with many pictures, so I decided to use a Doctrine Collection. The thing is, I want to retrieve this pictures and display them in my template... But it didn't seem to work. Do you know how I can do that ?

Here is what I tried :

{% for annonce in annonces %}
    <div class="annonce_item">
    {% for photo in annonce.photo  %}
        <img src="{{ photo.path }}" alt="" />
    {% endfor %}
</div>
<!-- End .annonce_item -->
{% endfor %}

annonce is the news class, and photo is the collection :

/**
 * @ORM\OneToMany(targetEntity="Photo", mappedBy="id",cascade={"persist"})
 */

private $photo;

When I try to display this page in my browser, I get this exception:

An exception has been thrown during the rendering of a template ("Notice: Undefined index: >id in >/Applications/MAMP/htdocs/ApacheImmobilier/vendor/doctrine/lib/Doctrine/ORM/Persisters/Basi>cEntityPersister.php line 1274") in "APPagesBundle:Index:index.html.twig" at line 45.

Thanks!

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

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

发布评论

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

评论(1

撩人痒 2024-12-25 17:47:41

阅读这篇文档。它说:

mappedBy 属性指定实体中的字段
关系的所有者。

在您的情况下,它必须是您的照片实体的 news 字段。

Read this article of the doc. It says:

The mappedBy attribute designates the field in the entity that is the
owner of the relationship.

which, in your case, must be the news field of your Photo entity.

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