gpt4 book ai didi

php - FatalErrorException : Error: Call to a member function getId() on a non-object in . ..PostListener.php 第 20 行

转载 作者:行者123 更新时间:2023-11-30 00:48:05 25 4
gpt4 key购买 nike

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

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

}

这是我的帖子存储库

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 扩展了事件{ 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());
}

这是我的操作(它打开帖子并提供创建评论的机会): 公共(public)函数 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

请帮助我。

最佳答案

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

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

关于php - FatalErrorException : Error: Call to a member function getId() on a non-object in . ..PostListener.php 第 20 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21206399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com