我正在研究Symfony2.3,当我尝试查看帖子时,我会得到这个例外。
错误 - 未接收的PHP异常 symfony component httpkernel exception notfoundhttpexception: " Blogbundle:找不到评论对象。"在 /.../blog/app/cache/dev/classes.php line 7168
我不知道我在哪里犯了一个错误,因为一切都很好,直到我开始使用vichuploaderbundle。但是评论实体和图像实体没有连接,因此可能不是原因。很奇怪的是,当我尝试查看使用VichuploaderBundle之前发表的帖子时,我没有得到例外。
如果有人可以帮助我,那就太好了。
这是我的代码:
帖子: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 %}
发布控制器视图:
/**
* 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()
)
);
}
评论实体:
namespace BlogBundleEntity;
/*use BlogBundleRepositoryPost;*/
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
use SymfonyComponentValidatorConstraints as Assert;
use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;
/**
* Class Comment.
*
* @package Model
* @author Monika Malinowska
*
* @ORMTable(name="comments")
* @ORMEntity(repositoryClass="BlogBundleRepositoryComment")
*/
class Comment
{
/**
* @ORMId
* @ORMColumn(
* type="integer",
* nullable=false,
* options={
* "unsigned" = true
* }
* )
* @ORMGeneratedValue(strategy="IDENTITY")
* @var integer $id
*/
private $id;
/**
* @ORMColumn(
* name="author",
* type="string",
* length=128,
* nullable=false
* )
* @var string $author
*/
protected $author;
/**
* @ORMColumn(
* name="content",
* type="text",
* nullable=false
* )
*
*/
protected $content;
/**
* @ORMColumn(
* name="created",
* type="datetime",
* nullable=false
* )
* @var DateTime $created
*/
protected $created;
/**
* @ORMManyToOne(targetEntity="Post", inversedBy="comments")
* @ORMJoinColumn(name="post_id", referencedColumnName="id")
*/
protected $post;
/**
* @ORMColumn(type="boolean")
*/
protected $approved;
/**
* Constructor.
*/
public function __construct()
{
$this->created = new DateTime();
$this->approved = false;
//$this->posts = new DoctrineCommonCollectionsArrayCollection();
}
/**
* 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;
}}
发布实体:
namespace BlogBundleEntity;
use DoctrineORMMapping as ORM;
use DoctrineCommonCollectionsArrayCollection;
use SymfonyComponentValidatorConstraints as Assert;
use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;
use SymfonyComponentHttpFoundationFileUploadedFile;
/**
* Class Post.
*
* @package Model
* @author Monika Malinowska
*
* @ORMTable(name="posts")
* @ORMEntity(repositoryClass="BlogBundleRepositoryPost")
*/
class Post
{
/**
* @ORMId
* @ORMColumn(
* type="integer",
* nullable=false,
* options={
* "unsigned" = true
* }
* )
* @ORMGeneratedValue(strategy="IDENTITY")
* @var integer $id
*/
private $id;
/**
* @ORMColumn(
* name="title",
* type="string",
* length=128,
* nullable=false
* )
* @var string $title
*/
protected $title;
/**
* @ORMColumn(
* name="content",
* type="text",
* nullable=false
* )
*
*/
protected $content;
/**
* @ORMColumn(
* name="created",
* type="datetime",
* nullable=false
* )
* @var DateTime $created
*/
protected $created;
/**
* @ORMColumn(
* name="updated",
* type="datetime",
* nullable=false
* )
* @var DateTime $updated
*/
protected $updated;
/**
* Tags array
*
* @ORMManyToMany(targetEntity="Tag")
* @ORMJoinTable(
* name="posts_tags",
* joinColumns={@ORMJoinColumn(name="post_id", referencedColumnName="id")},
* inverseJoinColumns={@ORMJoinColumn(name="tag_id", referencedColumnName="id")}
* )
*
* @var DoctrineCommonCollectionsArrayCollection $tags
*/
protected $tags;
/**
* @ORMOneToMany(targetEntity="Comment", mappedBy="post")
*
*/
protected $comments;
/**
* @ORMOneToMany(targetEntity="User", mappedBy="post")
*
*/
protected $users;
/**
* @ORMOneToOne(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 BlogBundleEntityTag $tags
*/
public function addTag(BlogBundleEntityTag $tags)
{
$this->tags[] = $tags;
}
/**
* Remove tags
*
* @param BlogBundleEntityTag $tags
*/
public function removeTag(BlogBundleEntityTag $tags)
{
$this->tags->removeElement($tags);
}
/**
* Get tags.
*
* @return DoctrineCommonCollectionsCollection
*/
public function getTags()
{
return $this->tags;
}
/**
* Set image.
*
* @param File|SymfonyComponentHttpFoundationFileUploadedFile $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 BlogBundleEntityImage $image
*/
public function addImage(BlogBundleEntityImage $image)
{
$this->image = $image;
}
/**
* Remove image
*
* @param BlogBundleEntityImage $image
*/
public function removeImage(BlogBundleEntityImage $image)
{
$this->$image->removeElement($image);
}
/**
* Add comments.
*
* @param BlogBundleEntityComment $comments
*/
public function addComment(BlogBundleEntityComment $comments)
{
$this->comments[] = $comments;
}
/**
* Remove comments
*
* @param BlogBundleEntityComment $comments
*/
public function removeComment(BlogBundleEntityComment $comments)
{
$this->comments->removeElement($comments);
}
/**
* Get comments.
*
* @return DoctrineCommonCollectionsCollection
*/
public function getComments()
{
return $this->comments;
}}
在ViewAction DocBlock中尝试删除行:
* @ParamConverter("comment", class="BlogBundle:Comment")