2017-08-04 68 views
1

我正在設置一個博客,並希望主頁顯示最新的博客條目,包括指向單個條目的鏈接。 我收到了列表中顯示的所有條目的鏈接,但我無法準確指出如何顯示單個條目。此外,我現在只要點擊鏈接,就會收到「找不到對象」異常。 這裏的控制器函數來顯示一個條目,其內容:Symfony 3找不到對象錯誤

<?php 
// src/BlogBundle/Controller/BlogController.php 
namespace BlogBundle\Controller; 

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; 
use BlogBundle\Entity\Blog; 
use BlogBundle\Entity\User; 
use BlogBundle\Form\BlogType; 
use Symfony\Component\HttpFoundation\Response; 
use Symfony\Component\HttpFoundation\Request; 

class BlogController extends Controller 
{ 
    /** 
     * @Route("/blog", name="bloglist", requirements={"page": "\d+"}) 
     */ 
    public function listAction() 
    { 
     $entry = $this->getDoctrine()->getRepository('BlogBundle:Blog')->findBy(array(), array('date' => 'DESC')); 
     dump($entry); 
     return $this->render('BlogBundle:blog:blog.html.twig', [ 
      'bloglist' => $entry 
     ]); 
    } 
    /** 
    * @Route("/blog/new", name="create") 
    */ 
    public function createAction(Request $request) { 
     $entry = new Blog(); 
     $entry->setDate(new \DateTime('now')); 
     $entry->setAuthor($this->getUser()); 
     $form = $this->createForm(BlogType::class, $entry); 
     $form->handleRequest($request); 

     if ($form->isSubmitted() && $form->isValid()) { 

     $entry = $form->getData(); 
     $em = $this->getDoctrine()->getManager(); 
     $em->persist($entry); 
     $em->flush(); 
     return $this->render('BlogBundle:blog:createsubmit.html.twig', array(
      'entry' => $entry, 
      'form' => $form->createView(), 
      'success' => true 
     )); 
     } 

     return $this->render('BlogBundle:blog:new.html.twig', array(
     'quote' => 'New blog entry created!', 
     'form' => $form->createView(), 
    )); 
    } 
    /** 
    * @Route("/blog/singleentry/{id}", name="singleentry", requirements={"id" = "\d+"}, defaults={"id" = 0}) 
    */ 
    public function listSingleEntryAction(Request $request, Blog $blog) 
    { 
     $em = $this->getDoctrine()->getRepository('BlogBundle:Blog')->find($blog); 
     $form = $this->createForm(BlogType::class, $entry); 
    $form->handleRequest($request); 
    $em->persist($entry); 
    $em->flush(); 

     return $this->render('BlogBundle:singleentry:edit.html.twig', array(
      'entry' =>$entry, 
      'form' => $form->createView() 
     )); 
     } 


    /** 
    * @Route("/blog/edit/{id}", name="entryedit", requirements={"id" = "\d+"}, defaults={"id" = 0}) 
    * 
    */ 
    public function editAction(Request $request, Blog $blog) { 
     $em = $this->getDoctrine()->getManager(); 
     $entry = $em->getRepository('BlogBundle:Blog')->find($blog); 
     if ($this->get('security.authorization_checker')->isGranted('ROLE_ADMIN') || $entry->getAuthor() == $this->getUser()->getUsername()) { 

     $form = $this->createForm(BlogType::class, $entry); 
     $form->handleRequest($request); 

     if ($form->isSubmitted() && $form->isValid()) { 

      $em->persist($entry); 
      $em->flush(); 

      return $this->render('BlogBundle:blog:editsubmit.html.twig', array(
      'entry' => $entry, 
      'form' => $form->createView(), 
      'success' => true 
      )); 
      } 

      return $this->render('BlogBundle:blog:edit.html.twig', array(
      'form' => $form->createView(), 
     )); 
     } 

     else { 
     echo '<div class="alert alert-danger alert-dismissable"> 
     <span aria-hidden="true" data-dismiss="alert" class="close">×</span> 
     <h4> 
     <i class="fa fa-exclamation-circle "></i> 
     Only the author of an entry is allowed to edit it!</h4></div>'; 

     return $this->listAction(); 
     } 
    } 
    /** 
    * @Route("/blog/delete/{id}", name="entrydelete", requirements={"id" = "\d+"}, defaults={"id" = 0}) 
    * 
    */ 
    public function deleteAction(Request $request, Blog $blog) { 
     $em = $this->getDoctrine()->getManager(); 
     $entry = $em->getRepository('BlogBundle:Blog')->find($blog); 
     if ($this->get('security.authorization_checker')->isGranted('ROLE_ADMIN') || $entry->getAuthor() == $this->getUser()->getUsername()) { 
      $em->remove($entry); 
      $em->flush(); 
      return $this->render('BlogBundle:blog:deletesubmit.html.twig'); 
     } 
     else { 
     return $this->render('BlogBundle:blog:error.html.twig'); 
     } 
    } 
    public function configureOptions(OptionsResolver $resolver) { 
     $resolver->setDefaults([ 
     'data_class' => 'BlogBundle\Entity\Blog' 
     ]); 
    } 
} 
?> 

我的主頁:

<?php 

namespace BlogBundle\Controller; 

use Symfony\Bundle\FrameworkBundle\Controller\Controller; 
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; 


class DefaultController extends Controller { 
    /** 
    * @Route("/", name="welcome") 
    */ 
    public function indexAction() { 
    $entry = $this->getDoctrine()->getRepository('BlogBundle:Blog')->findBy(array(), array('date' => 'DESC'),5); 
    dump($entry); 
    return $this->render('BlogBundle:Default:index.html.twig', [ 
     'Welcome' => 'Welcome!', 
     'avatar_url' => 'http://www.lufthansa.com/mediapool/jpg/45/media_789050645.jpg', 
     'blog'  => $this->redirectToRoute('singleentry'), 
     'bloglist' => $entry 
      ]); 
    } 
} 
?> 

而這些都是樹枝模板,我嘗試呈現

singleentry.html。小枝:

{% extends '::base.html.twig' %} 

{% block body %} 

<div class="container"> 
    <div class="col-md-11"> 
      <h1 class="page-header">{{ blog. title }}</h1> 
     <div class="table"> 
      <p><span class="fa fa-clock-o"></span> Posted on {{ blog.date|date('d.M Y H:i A') }} </p> 
      <p><span class="fa fa-user-circle"></span> Posted by {{ blog.author }} </p> 
      <p>{{ blog.text }}</p> 
      <!-- <a class="btn btn-primary" href="#">Read More <span class="glyphicon glyphicon-chevron-right"></span></a> --> 
      <button type="button" class="btn btn btn-info"> 
       <a href="{{ path('entryedit', {'id':blog.id}) }}" style="color: #FEFEFE">Edit entry</a> 
      </button> 
      <button type="button" class="btn btn btn-warning"> 
       <a href="{{ path('entrydelete', {'id':blog.id}) }}" style="color: #FEFEFE">Delete entry</a> 
      </button> 
      <hr> 
     </div> 
    </div> 
</div> 
{% endblock %} 

我的主頁上的博客條目列表:

<div class="panel panel-default"> 
    <div class="panel-heading"> 
    <h3 class="panel-title text-center">My latest blog entries</h3> 
    </div> 
    <table class="table"> 
    {% for blog in bloglist %} 
      <tr> 
       <td><a href="{{ path('singleentry') }}">{{ blog.title }}</a></td> 
       <td width="85%" style="max-width: 0; 
       overflow: hidden; 
       text-overflow: ellipsis; 
       white-space: nowrap;"> 
       {{ blog.text }} 
       </td> 
      </tr> 
     {% endfor %} 
     </table> 
</div> 

編輯版本listSingleEntryAction的 - >對象顯示爲NULL

public function listSingleEntryAction(Request $request, $id) 
{ 
    $em = $this->getDoctrine()->getManager(); 
    $entry = $em->getRepository('BlogBundle:Blog')->find($id); 
    if($entry == null) 
    { 
     $message='Entry does not exist'; 
     return $this->render('BlogBundle:blog:error.html.twig'); 
    } 
    return $this->render('BlogBundle:blog:singleentry.html.twig', Array(
     'entry' => $entry, 
     'success' => true, 
)); 


    } 

回答

0

的問題是你的論點。

public function listSingleEntryAction(Request $request, Blog $blog) 
    { 

首先,它不應該與博客類型。這是int。 其次,將其重命名爲$ id。

而且我認爲你的編輯和刪除操作有同樣的問題。

+0

我試過了,但它沒有改變一件事,不幸的是!實際上,我的編輯和刪除操作正在工作:/ – sonja

+0

我現在改變了它,但每次嘗試選擇對象時都說對象不存在。 – sonja

+0

另一個問題是find方法會返回一個實體。您將它分配給em變量,然後使用條目。我認爲你應該改變進入 – Vladislav

0

檢查你的動作。前兩行是你問題的原因。

/** 
    * @Route("/blog/singleentry/{id}", name="singleentry", requirements={"id" = "\d+"}, defaults={"id" = 0}) 
    */ 
    public function listSingleEntryAction(Request $request, Blog $blog) 
    { 
     $em = $this->getDoctrine()->getManager(); 
     $entry = $em->getRepository('BlogBundle:Blog')->find($blog);