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

时间:2014-01-18 15:58:37

标签: php mysql symfony

我是Symfony2的新手。我有创建博客的任务。其中一个必要的是显示最受欢迎的帖子。所以我认为最好的变量是创建监听器。当访问者阅读帖子时,它将被呼叫。并且监听器将增加数据库(MySQL)中的fild。我在存储库中创建方法,通过该字段进行选择。还可以创建Action,通过此选择呈现帖子。但是当我尝试阅读帖子时,有一个错误:

FatalErrorException:错误:在/var/www/blo/src/Blog/Bundle/BlogBu​​ndle/EventListener/PostVisitedListener.php第20行中的非对象上调用成员函数getId()。

请帮助我。

这是我的实体(帖子):

namespace Blog\Bundle\BlogBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Gedmo\Mapping\Annotation as Gedmo;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * Post
 *
 * @ORM\Table(name="post")
 * @ORM\Entity(repositoryClass="Blog\Bundle\BlogBundle\Entity\PostRepository")
 */
class Post
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

/**
 * @var string
 *
 * @ORM\Column(name="title", type="string", length=255)
 * @Assert\NotBlank
 * @Assert\Length(min="13", max="255")
 */
 private $title;

    /**
     * @var string
     *
     * @ORM\Column(name="author", type="string", length=100)
     * @Assert\NotBlank
     * @Assert\Length(min="13", max="100")
     */
 private $author;

/**
 * @var string
 *
 * @ORM\Column(name="post", type="text")
 * @Assert\NotBlank
 * @Assert\Length(min="100")
 */
private $post;

/**
 * @var string
 *
 * @ORM\Column(name="image", type="string", length=100)
 */
private $image;

/**
 * @Gedmo\Timestampable(on="create")
 * @ORM\Column(name="createdAt", type="datetime")
 *
 */
private $createdAt;

/**
 *
 * @ORM\Column(name="tags", type="text")
 */
private $tags;

/**
 * @ORM\ManyToOne(targetEntity="Category", inversedBy="posts")
 */
private $category;

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


/**
 * @var integer
 *
 * @ORM\Column(name="visitedIncrement", type="integer")
 */

private $visitedIncrement;


public function __construct()
{
    $this->comments = new ArrayCollection();
}
/**
 * Get id
 *
 * @return integer
 */
public function getId()
{
    return $this->id;
}

/**
 * Set title
 *
 * @param string $title
 * @return Post
 */
public function setTitle($title)
{
    $this->title = $title;

    return $this;
}

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

/**
 * Set author
 *
 * @param string $author
 * @return Post
 */
public function setAuthor($author)
{
    $this->author = $author;

    return $this;
}

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

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

    return $this;
}

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

/**
 * Set image
 *
 * @param string $image
 * @return Post
 */
public function setImage($image)
{
    $this->image = $image;

    return $this;
}

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

/**
 * Set tags
 *
 * @param string $tags
 * @return Post
 */
public function setTags($tags)
{
    $this->tags = $tags;

    return $this;
}

/**
 * Get tags
 *
 * @return string
 */
public function getTags()
{
    return $this->tags;
}

/**
 * Set category
 *
 * @param $category
 * @return $this
 */
public function setCategory($category)
{
    $this->category = $category;

    return $this;
}

/**
 *  Get category
 *
 * @return integer
 */
public function getCategory()
{
    return $this->category;
}

/**
 * Set comments
 *
 * @param string $comments
 * @return Post
 */
public function setComments($comments)
{
    $this->comments = $comments;

    return $this;
}

/**
 * Get comments
 *
 * @return string
 */
public function getComments()
{
    return $this->comments;
}

/**
 * @param \DateTime $createdAt
 */
public function setCreatedAt($createdAt)
{
    $this->createdAt = $createdAt;

    return $this;
}

/**
 * @return \DateTime
 */
public function getCreatedAt()
{
    return $this->createdAt;
}

/**
 * @param int $visitedIncrement
 */
public function setVisitedIncrement($visitedIncrement)
{
    $this->visitedIncrement = $visitedIncrement;

    return $this;
}

/**
 * @return int
 */
public function getVisitedIncrement()
{
    return $this->visitedIncrement;
}

public function __toString()
{
    return $this->getTitle();
}

}

这是我的PostRepository

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();

这是我的PostVisitedEvent     

命名空间Blog \ Bundle \ BlogBu​​ndle \ Event;

使用Blog \ Bundle \ BlogBu​​ndle \ Entity \ Post; 使用Symfony \ Component \ EventDispatcher \ Event;

类PostVisitedEvent扩展了Event {     protected $ post;

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


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

} 

这是我的PostVisitedListener

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 = $ postRepository-> find($ id);

    if (!$post) {
        throw $this->createNotFoundException('The post is not found!');
    }
    $commentRepository = $this->container->get('blog_blog_bundle.comment.repository');
    $comments = $commentRepository->findCommentForPost($post->getId());

    $event = new PostVisitedEvent();
    $event->setPost($post);

    $eventDispatcher = $this->get('event_dispatcher');
    $eventDispatcher->dispatch('blog_blog_bundle.post_visited', $event);

    return $this->render('BlogBlogBundle:Default:showPost.html.twig', array(
        'post' => $post,
        'comments' => $comments,
    ));
}

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

请帮助我。

1 个答案:

答案 0 :(得分:0)

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

PostInterface不是symfony接口。你必须编码。请求接口比询问具体实例更好,因为symfony有时使用代理类而不是具体类。