Symfony4:实体将repositoryClass设置为"App\Entity\CommentRepository",但这不是有效的类

sim*_*lay 2 php repository symfony doctrine-orm

在Symfony4中我有一个存储库:

<?php

namespace App\Repository;

use App\Entity\Comment;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Symfony\Bridge\Doctrine\RegistryInterface;

class CommentRepository extends ServiceEntityRepository
{
    public function __construct(RegistryInterface $registry)
    {
        parent::__construct($registry, Comment::class);
    }

    public function findByNews($news)
    {
        return $this->createQueryBuilder('c')
            ->where('c.news = :news')
            ->setParameter('news', $news)
            //->setMaxResults(10)
            ->getQuery()
            ->getResult()
        ;
    }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试在我的ajax控制器的这个动作中使用它时:

public function comments(Request $request)
    {
        if ($request->isXmlHttpRequest()) {
            $newsId = $request->get('id');

            $newsRepository = $this->getDoctrine()->getRepository(News::class);
            $news = $newsRepository->find($newsId);
            $commentRepository = $this->getDoctrine()->getRepository(Comment::class);
            $comments = $commentRepository->findByNews($news);

            $comments = 0;

            $serializer = $this->get('serializer');
            $response = $serializer->serialize($comments, 'json');

            return new JsonResponse(array('data' => $response));
        }

        return new Response("Error : this is not an ajax request!", 400);
    }
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

未捕获的PHP异常RuntimeException:""App\Entity\Comment"实体将repositoryClass设置为"App\Entity\CommentRepository",但这不是一个有效的类.请检查您的类命名.如果这是一个服务ID ,确保此服务存在并标记为"doctrine.repository_service"." at(...)\ vendor\doctrine\doctrine-bundle\Repository\ContainerRepositoryFactory.php第82行

我不明白为什么CommentRepository无效.

为什么要提这App\Entity\CommentRepository而不是App\Repository\CommentRepository

任何的想法 ?

编辑:

这是评论实体:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * MemberBundle\Entity\Comment
 *
 * @ORM\Table(name="comment")
 * @ORM\Entity(repositoryClass="App\Entity\CommentRepository")
 */
class Comment
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var App\Entity\News $news
     *
     * @ORM\ManyToOne(targetEntity="App\Entity\News")
     * @ORM\JoinColumn(name="news", referencedColumnName="id", onDelete="CASCADE")
     */
    private $news;

    /**
     * @var App\Entity\User $author
     *
     * @ORM\ManyToOne(targetEntity="App\Entity\User")
     * @ORM\JoinColumn(name="author", referencedColumnName="id", onDelete="CASCADE")
     */
    private $author;

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

    /**
     * @var datetime $date
     *
     * @ORM\Column(name="date", type="datetime")
     */
    private $date;

    public function __construct() {
        $this->date = new \DateTime();
    }


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

    /**
     * Set news
     *
     * @param App\Entity\News $news
     */
    public function setNews($news)
    {
        $this->news = $news;
    }

    /**
     * Get news
     *
     * @return App\Entity\News
     */
    public function getNews()
    {
        return $this->news;
    }

    /**
     * Set author
     *
     * @param App\Entity\User $author
     */
    public function setAuthor($author)
    {
        $this->author = $author;
    }

    /**
     * Get author
     *
     * @return App\Entity\User
     */
    public function getAuthor()
    {
        return $this->author;
    }

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

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

    /**
     * Set date
     *
     * @param datetime $date
     */
    public function setDate($date)
    {
        $this->date = $date;
    }

    /**
     * Get date
     *
     * @return datetime 
     */
    public function getDate()
    {
        return $this->date;
    }
}
Run Code Online (Sandbox Code Playgroud)

小智 8

你应该@ORM\Entity(repositoryClass="App\Entity\CommentRepository")改为 @ORM\Entity(repositoryClass="App\Repository\CommentRepository")

否则您可以CommentRepository进入Entity目录(并相应地更新命名空间),但最好遵循标准结构并将您的存储库保存在App\Repository命名空间中.

  • 不要将存储库类移动到您的实体目录,因为这样做不是一个好习惯 (4认同)