具有键"id, title, artext, createdat, updatedat, position, status"的数组的键"image"不存在



我正在Symfony 5.4上工作,并有以下错误

Key "image" for array with keys "id, title, status" does not exist.

然后我有两个表:Image文章和并需要张贴和绘制文章,每篇文章与他的图像。

文章库

public function findbyLast() {
$qb = $this->createQueryBuilder('a');
$qb->andWhere('a.status = 1');
$qb->orderBy('a.id', 'DESC');
$qb->setMaxResults( 10 );
return  $qb->getQuery()->getArrayResult();
}
<<p>文章控制器/strong>
#[Route('/articles', name: 'articles')]
public function articles(ArticleRepository $articles): Response
{
return $this->render('main/articles.html.twig', [
'articles' => $articles->findbyLast() ,
]);
}

View on Twig

{% for x in articles %}
<a href="{{ path('app_article_show', {'id': x.id}) }}">
<h4>{{ x.title }}</h4>
{% if x.image %}
<img src="{{ asset('images/articles/'~x.image )}}" alt="{{ x.image.name }}" class="img-fluid row">
{% endif %}

{% endfor %}

我一直在努力,不知道如何管理我的存储库加入并拥有丢失的密钥。

谢谢你的帮助!

bug已解决。我没有区分

存储库中的getResult和getArrayResult。Get Result呈现一个对象列表,另一个呈现一个数组。

最新更新