FatalErrorException:错误:对…PostListener.php第20行中的非对象调用成员函数getId()

FatalErrorException:错误:对…PostListener.php第20行中的非对象调用成员函数getId(),php,mysql,symfony,Php,Mysql,Symfony,我是Symfony2的新手。我有一项创建博客的任务。其中一个必要条件是展示最受欢迎的帖子。所以我认为最好的变体是创建监听器。当访问者阅读帖子时,它将被调用。监听器将在数据库(MySQL)中的fild上增加。我在repository中创建了一个方法,它通过这个字段进行选择。以及创建操作,该操作通过此选择呈现帖子。但当我试图阅读帖子时,出现了一个错误: FatalErrorException:错误:在/var/www/blo/src/Blog/Bundle/BlogBundle/EventListe

我是Symfony2的新手。我有一项创建博客的任务。其中一个必要条件是展示最受欢迎的帖子。所以我认为最好的变体是创建监听器。当访问者阅读帖子时,它将被调用。监听器将在数据库(MySQL)中的fild上增加。我在repository中创建了一个方法,它通过这个字段进行选择。以及创建操作,该操作通过此选择呈现帖子。但当我试图阅读帖子时,出现了一个错误:

FatalErrorException:错误:在/var/www/blo/src/Blog/Bundle/BlogBundle/EventListener/PostVisitedListener.php第20行对非对象调用成员函数getId()

请帮帮我

public function onPostVisited(PostVisitedEvent $event)
{
    if (!($event->getPost() instanceof PostInterface)) return;
    $this->repository->visitedIncrement($event->getPost()->getId());
}
这是我的实体(职位):

这是我的邮件存储库

public function visitedIncrement($id)
{
    $query = $this->getEntityManager()
        ->createQuery(
            'UPDATE BlogBlogBundle:Post p
             SET p.visitedIncrement = p.visitedIncrement + 1
             WHERE p.id = :post_id')
        ->setParameter(':post_id', $id);

    $query->execute();
这是我的追访活动 命名空间Blog\Bundle\BlogBundle\Event

使用Blog\Bundle\BlogBundle\Entity\Post; 使用Symfony\Component\EventDispatcher\Event

类PostVisitedEvent扩展事件 { 受保护的$员额

/**
 * @param Post $post
 */
public function setPost(Post $post)
{
    return $this->post;
}


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

} 
这是我的追访听众

namespace Blog\Bundle\BlogBundle\EventListener;

use Blog\Bundle\BlogBundle\Entity\PostRepository;
use Doctrine\ORM\EntityManager;
use Blog\Bundle\BlogBundle\Event\PostVisitedEvent;

class PostVisitedListener
{
protected $repository;

public function __construct(PostRepository $repository)
{
    $this->repository = $repository;
}

public function onPostVisited(PostVisitedEvent $event)
{
    $this->repository->visitedIncrement($event->getPost()->getId());
}
这是我的行动(它打开帖子并提供创建评论的机会): 公共功能showPostAction($id) { $postRepository=$this->container->get('blog_blog_bundle.post.repository'); $post=$post存储库->查找($id)

Yuo可以看到,我还为存储库和侦听器创建了服务

service id="blog_blog_bundle.post.repository"  class="Blog\Bundle\BlogBundle\Entity\PostRepository" factory-service="doctrine.orm.entity_manager" factory-method="getRepository"
        argument>BlogBlogBundle:Post argument
    service

    service id="blog_blog_bundle.comment.repository"       class="Blog\Bundle\BlogBundle\Entity\CommentRepository" factory-service="doctrine.orm.entity_manager" factory-method="getRepository"
        argument BlogBlogBundle:Comment argument
    service

    service id="blog_blog_bundle.post_visited_listener" class="Blog\Bundle\BlogBundle\EventListener\PostVisitedListener"
        argument type="service" id="blog_blog_bundle.post.repository" 
        tag name="kernel.event_listener" event="blog_blog_bundle.post_visited" method="onPostVisited" 
    service
请帮帮我

public function onPostVisited(PostVisitedEvent $event)
{
    if (!($event->getPost() instanceof PostInterface)) return;
    $this->repository->visitedIncrement($event->getPost()->getId());
}
PostInterface不是symfony接口。您必须对其进行编码。请求接口比请求具体实例要好,因为symfony有时使用代理类而不是具体类

public function onPostVisited(PostVisitedEvent $event)
{
    if (!($event->getPost() instanceof PostInterface)) return;
    $this->repository->visitedIncrement($event->getPost()->getId());
}