Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony4 : entity has a repositoryClass set to "App\Entity\CommentRepository", but this is not a valid class

In Symfony4 I have a repository :

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

When I try to use it in this action of my ajax controller:

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

I get this error :

Uncaught PHP Exception RuntimeException: "The "App\Entity\Comment" entity has a repositoryClass set to "App\Entity\CommentRepository", but this is not a valid class. Check your class naming. If this is meant to be a service id, make sure this service exists and is tagged with "doctrine.repository_service"." at (...)\vendor\doctrine\doctrine-bundle\Repository\ContainerRepositoryFactory.php line 82

I can't see why the CommentRepository is not valid.

And why this is mentionning App\Entity\CommentRepository instead of App\Repository\CommentRepository?

Any idea ?

Edit :

Here's the Comment entity :

<?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;
    }
}
like image 413
simslay Avatar asked Dec 17 '17 18:12

simslay


1 Answers

You should change @ORM\Entity(repositoryClass="App\Entity\CommentRepository") to @ORM\Entity(repositoryClass="App\Repository\CommentRepository")

You could otherwise move CommentRepositoryinto Entity directory (and update the namespace accordingly), but it's best to follow the standard structure and keep your repositories in the App\Repository namespace.

like image 108
Mert Simsek Avatar answered Sep 30 '22 18:09

Mert Simsek