doulan8152 2016-06-22 12:00
浏览 60
已采纳

例外:BlogBu​​ndle:找不到评论对象

I'm working on Symfony2.3 and I get this exception when I'm trying to view a post.

ERROR - Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "BlogBundle:Comment object not found." at /.../blog/app/cache/dev/classes.php line 7168

I don't know where I made a mistake because everything was fine until I've started to use VichUploaderBundle. But the Comment entity and Image entity are not connected so probably it's not the cause. And weird thing is that I don't get the exception when I'm trying to view posts which I made before using VichUploaderBundle.

It would be great if someone could help me.

Here is my code:

Post: view.html.twig

{% block body %}
<article class="blog">
    <header>
        <div class="date">
            <time datetime="{{ post.created|date('c') }}">{{ post.created|date('l, F j, Y') }}</time>
        </div>
        <h2>{{ post.title }}</h2>
    </header>

    {% if post.image.imageName is not null %}
        <img src='{{ asset("assetic/images/"~ post.image.imageName) }}'/>
    {% endif %}

    <div>
        <p>{{ post.content }}</p>
    </div>
    <div>
        {% for tag in post.tags %}
            <p>Tagi: <a href="{{ url('tags-view', {'id': tag.id }) }}" title="">{{ tag.name }}</a></p>
        {% endfor %}
    </div>

</article>

<section class="comments" id="comments">
    <section class="previous-comments">
        <h3>Komentarze:</h3>
        {% if comments|length %}
            {% include 'BlogBundle:Comments:index.html.twig' with {  'comments': comments } %}
        {% else %}
            <p>
                Brak komentarzy
            </p>
        {% endif %}
    </section>
</section>

<p><a href={{ url ("comments-add", {'id': post.id}) }}>Dodaj komentarz </a></p>{% endblock %}

Post Controller view:

    /**
 * View action.
 *
 * @Route("/posts/view/{id}", name="posts-view")
 * @Route("/posts/view/{id}/")
 * @ParamConverter("post", class="BlogBundle:Post")
 * @ParamConverter("comment", class="BlogBundle:Comment")
 *
 * @param Post|int $id Element id
 * @return Response
 * @internal param Comments $comments
 */
public function viewAction(Post $id)
{
    $post = $this->postsModel->findOneById($id);
    if (!$post) {
        throw new NotFoundHttpException('Post not found!');
    }

    $comments = $this->commentsModel->getCommentsForPost($id);

    return $this->templating->renderResponse(
        'BlogBundle:Posts:view.html.twig',
        array(
            'post' => $post,
            'comments' => $comments
            //'form' => $commentForm->createView()
        )
    );
}

Comment Entity:

namespace BlogBundle\Entity;

/*use BlogBundle\Repository\Post;*/
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
 * Class Comment.
 *
 * @package Model
 * @author Monika Malinowska
 *
 * @ORM\Table(name="comments")
 * @ORM\Entity(repositoryClass="BlogBundle\Repository\Comment")
 */
class Comment
{
/**
 * @ORM\Id
 * @ORM\Column(
 *     type="integer",
 *     nullable=false,
 *     options={
 *         "unsigned" = true
 *     }
 * )
 * @ORM\GeneratedValue(strategy="IDENTITY")
 * @var integer $id
 */
private $id;

/**
 * @ORM\Column(
 *     name="author",
 *     type="string",
 *     length=128,
 *     nullable=false
 * )
 * @var string $author
 */
protected $author;

/**
 * @ORM\Column(
 *     name="content",
 *     type="text",
 *     nullable=false
 * )
 *
 */
protected $content;

/**
 * @ORM\Column(
 *     name="created",
 *     type="datetime",
 *     nullable=false
 * )
 * @var \DateTime $created
 */
protected $created;

/**
 * @ORM\ManyToOne(targetEntity="Post", inversedBy="comments")
 * @ORM\JoinColumn(name="post_id", referencedColumnName="id")
 */
protected $post;

/**
 * @ORM\Column(type="boolean")
 */
protected $approved;

/**
 * Constructor.
 */
public function __construct()
{
    $this->created = new \DateTime();
    $this->approved = false;

    //$this->posts = new \Doctrine\Common\Collections\ArrayCollection();
}

/**
 * Set approved.
 *
 * @param boolean $approved
 */
public function setApproved($approved)
{
    $this->approved = $approved;
}

/**
 * Get approved.
 *
 * @return boolean
 */
public function getApproved()
{
    return $this->approved;
}

/**
 * Set Id.
 *
 * @param integer $id Id
 */
public function setId($id)
{
    $this->id = $id;
}

/**
 * Get Id.
 *
 * @return integer Result
 */
public function getId()
{
    return $this->id;
}

/**
 * Set content.
 *
 * @param string $content Content
 */
public function setContent($content)
{
    $this->content = $content;
}

/**
 * Get content.
 *
 * @return string Content
 */
public function getContent()
{
    return $this->content;
}

/**
 * Set author.
 *
 * @param string $author Author
 */
public function setAuthor($author)
{
    $this->author = $author;
}

/**
 * Get content.
 *
 * @return string Content
 */
public function getAuthor()
{
    return $this->author;
}

/**
 * Get created.
 *
 * @return \DateTime Created
 */
public function getCreated()
{
    return $this->created;
}

/**
 * Set created.
 *
 * @param \DateTime $created Created
 */
public function setCreated($created)
{
    $this->created = $created;
}

/**
 * Set Post.
 *
 * @param integer $post Post
 */
public function setPost(Post $post)
{
    $this->post = $post;
}

/**
 * Get Post.
 *
 * @return integer Result
 */
public function getPost()
{
    return $this->post;
}}

Post Entity:

namespace BlogBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
 * Class Post.
 *
 * @package Model
 * @author Monika Malinowska
 *
 * @ORM\Table(name="posts")
 * @ORM\Entity(repositoryClass="BlogBundle\Repository\Post")
 */
class Post
{
/**
 * @ORM\Id
 * @ORM\Column(
 *     type="integer",
 *     nullable=false,
 *     options={
 *         "unsigned" = true
 *     }
 * )
 * @ORM\GeneratedValue(strategy="IDENTITY")
 * @var integer $id
 */
private $id;

/**
 * @ORM\Column(
 *     name="title",
 *     type="string",
 *     length=128,
 *     nullable=false
 * )
 * @var string $title
 */
protected $title;

/**
 * @ORM\Column(
 *     name="content",
 *     type="text",
 *     nullable=false
 * )
 *
 */
protected $content;
/**
 * @ORM\Column(
 *     name="created",
 *     type="datetime",
 *     nullable=false
 * )
 * @var \DateTime $created
 */
protected $created;

/**
 * @ORM\Column(
 *     name="updated",
 *     type="datetime",
 *     nullable=false
 * )
 * @var \DateTime $updated
 */
protected $updated;

/**
 * Tags array
 *
 * @ORM\ManyToMany(targetEntity="Tag")
 * @ORM\JoinTable(
 *      name="posts_tags",
 *      joinColumns={@ORM\JoinColumn(name="post_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="tag_id", referencedColumnName="id")}
 * )
 *
 * @var \Doctrine\Common\Collections\ArrayCollection $tags
 */
protected $tags;

/**
 * @ORM\OneToMany(targetEntity="Comment", mappedBy="post")
 *
 */
protected $comments;

/**
 * @ORM\OneToMany(targetEntity="User", mappedBy="post")
 *
 */
protected $users;


/**
 * @ORM\OneToOne(targetEntity="Image", mappedBy="post")
 */
protected $image;

/**
 * Constructor.
 */
public function __construct()
{
    $this->tags = new ArrayCollection();
    $this->comments = new ArrayCollection();
    $this->created= new \DateTime();
    $this->updated= new \DateTime();
}


/**
 * Get Id.
 *
 * @return integer Result
 */
public function getId()
{
    return $this->id;
}

/**
 * Set title.
 *
 * @param string $title Title
 */
public function setTitle($title)
{
    $this->title = $title;
}

/**
 * Get title.
 *
 * @return string Title
 */
public function getTitle()
{
    return $this->title;
}

/**
 * Set Id.
 *
 * @param integer $id Id
 */
public function setId($id)
{
    $this->id = $id;
}

/**
 * Set content.
 *
 * @param string $content Content
 */
public function setContent($content)
{
    $this->content = $content;
}

/**
 * Get content.
 *
 * @return string Content
 *
 * public function getContent()
 * {
 * return $this->content;
 * } */
public function getContent($length = null)
{
    if (false === is_null($length) && $length > 0) {
        return substr($this->content, 0, $length);
    } else {
        return $this->content;
    }
}

/**
 * Get created.
 *
 * @return integer Created
 */
public function getCreated()
{
    return $this->created;
}

/**
 * Set created.
 *
 * @param string $created Created
 */
public function setCreated($created)
{
    $this->created = $created;
}

/**
 * Get updated.
 *
 * @return string Updated
 */
public function getUpdated()
{
    return $this->updated;
}

/**
 * Set updated.
 *
 * @param string $updated Updated
 */
public function setUpdated($updated)
{
    $this->updated = $updated;
}

/**
 * Add tags.
 *
 * @param \BlogBundle\Entity\Tag $tags
 */
public function addTag(\BlogBundle\Entity\Tag $tags)
{
    $this->tags[] = $tags;
}

/**
 * Remove tags
 *
 * @param \BlogBundle\Entity\Tag $tags
 */
public function removeTag(\BlogBundle\Entity\Tag $tags)
{
    $this->tags->removeElement($tags);
}

/**
 * Get tags.
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getTags()
{
    return $this->tags;
}


/**
 * Set image.
 *
 * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
 * @return $image
 */
public function setImage($image)
{
    $this->image = $image;
    return $this;
}

/**
 * Get image.
 *
 * @return file Image
 */
public function getImage()
{
    return $this->image;
}

/**
 * Add images.
 *
 * @param \BlogBundle\Entity\Image $image
 */
public function addImage(\BlogBundle\Entity\Image $image)
{
    $this->image = $image;
}

/**
 * Remove image
 *
 * @param \BlogBundle\Entity\Image $image
 */
public function removeImage(\BlogBundle\Entity\Image $image)
{
    $this->$image->removeElement($image);
}

/**
 * Add comments.
 *
 * @param \BlogBundle\Entity\Comment $comments
 */
public function addComment(\BlogBundle\Entity\Comment $comments)
{
    $this->comments[] = $comments;
}

/**
 * Remove comments
 *
 * @param \BlogBundle\Entity\Comment $comments
 */
public function removeComment(\BlogBundle\Entity\Comment $comments)
{
    $this->comments->removeElement($comments);
}

/**
 * Get comments.
 *
 * @return \Doctrine\Common\Collections\Collection
 */
public function getComments()
{
    return $this->comments;
}}
  • 写回答

1条回答 默认 最新

  • drfif48428 2016-06-22 12:17
    关注

    In viewAction docblock try and remove line:

    * @ParamConverter("comment", class="BlogBundle:Comment")
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题