我收到此错误,无法弄清楚如何解决它。想法是列出用户的所有书籍。使用用户ID调用时的路径工作正常,例如:userbooks / 4。但使用用户名userbooks / paul调用我得到此错误"无法访问空变量的属性("用户")。"
<h2 > <a href="{{ path('userbooks',{'user' : entry.user}) }} ">{{ entry.title | title }}</a></h2>
viewuserbookAction:
public function viewuserbooksAction($user)
{
$em = $this->getDoctrine()->getManager();
$book = $em->getRepository('BookReviewBookBundle:Book')->find($user);
return $this->render('@BookReviewBook/Book/viewuserbooks.html.twig',
['book' => $book]);
}
viewuserbook.html.twig
{% extends '@BookReviewBook/layout.html.twig' %}
{% block title %}{% endblock %}
{% block body %}
{% for book in book.user.entries %}
<h1>{{ book.title }} </h1>
<div>
<img class="img-valign" src="{{ asset('images/book/'~ book.path ) }}" , style
= "width:300px;height:300px;" class="img-thumbnail " />
<span class="text2">{{ book.author }}</span>
{{ book.averageRating |rating }}
</div>
<hr class="style4">
<p>{{ book.summary|nl2br }} </p>
<p><small>Posted by {{ book.user }} on {{ book.timestamp|date('H:i d/m/y') }}
</small>
</p>
<hr class="style4">
{% endfor %}
的routing.yml
userbooks:
path: /userbooks/{user}
defaults: { _controller: BookReviewBookBundle:User:viewuserbooks }
view:
path: /view/{id}
defaults: { _controller: BookReviewBookBundle:Book:view }
requirements:
id: \d+
答案 0 :(得分:0)
谢谢大家的回复和帮助。现在工作得很好。必须通过用户名
找到viewuserbookAction:
public function viewuserbooksAction($username)
{
$em = $this->getDoctrine()->getManager();
$book = $em->getRepository('UserBundle:User')->find($username);
return $this->render('@BookReviewBook/Book/viewuserbooks.html.twig',
['user' => $book]);
}
viewuserbook.html.twig
{% extends '@BookReviewBook/layout.html.twig' %}
{% block title %}{% endblock %}
{% block body %}
{% for book in user.entries %}
<h1>{{ book.title }} </h1>
<div>
<img class="img-valign" src="{{ asset('images/book/'~ book.path ) }}" ,
style
= "width:300px;height:300px;" class="img-thumbnail " />
<span class="text2">{{ book.author }}</span>
{{ book.averageRating |rating }}
</div>
<hr class="style4">
<p>{{ book.summary|nl2br }} </p>
<p><small>Posted by {{ book.user }} on {{ book.timestamp|date('H:i d/m/y')
}}
</small>
</p>
{% endfor %}
{% endblock %}
的routing.yml
userbooks:
path: /userbooks/{username}
defaults: { _controller: BookReviewBookBundle:User:viewuserbooks }
index.html.twig
<h2 > <a href="{{ path('userbooks',{ username : entry.user.id}) }} ">{{
entry.title | title }}</a></h2>