简体   繁体   English

无法访问null变量上的属性(“用户”)

[英]Impossible to access an attribute (“user”) on a null variable

I am getting this error and cant figure out how to solve it. 我收到此错误,无法解决。 The idea is to list all books of a user. 想法是列出用户的所有书籍。 the path when called with the ID of the user works fine for example : userbooks/4 . 使用用户ID调用时,路径可以正常工作,例如:userbooks / 4。 but called the with the username userbooks/paul i get this error "Impossible to access an attribute ("user") on a null variable." 但用用户名userbooks / paul调用时,出现此错误“无法访问null变量上的属性(“用户”)。”

   <h2 > <a href="{{ path('userbooks',{'user' : entry.user}) }} ">{{ entry.title | title }}</a></h2>    

viewuserbookAction: viewuserbook动作:

  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 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 routing.yml

 userbooks:
     path: /userbooks/{user}
     defaults: { _controller: BookReviewBookBundle:User:viewuserbooks }

  view:
     path: /view/{id}
     defaults: { _controller: BookReviewBookBundle:Book:view }
     requirements:
       id: \d+

Thank you all for the reply and help. 谢谢大家的答复和帮助。 Works perfectly now. 现在可以完美运行。 had to find by username 必须通过用户名查找

viewuserbookAction: viewuserbook动作:

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 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 routing.yml

userbooks:
     path: /userbooks/{username}
     defaults: { _controller: BookReviewBookBundle:User:viewuserbooks }

index.html.twig index.html.twig

  <h2 > <a href="{{ path('userbooks',{ username : entry.user.id}) }} ">{{ 
  entry.title | title }}</a></h2>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM