where('c.news = :news') ->set-6ren">
gpt4 book ai didi

php - Symfony 4 : entity has a repositoryClass set to "App\Entity\CommentRepository", 但这不是有效的类

转载 作者:行者123 更新时间:2023-12-02 17:59:17 26 4
gpt4 key购买 nike

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

当我尝试在我的 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);
}

我收到此错误:

未捕获的 PHP 异常 RuntimeException:““App\Entity\Comment”实体的repositoryClass 设置为“App\Entity\CommentRepository”,但这不是一个有效的类。请检查您的类命名。如果这意味着服务 ID,确保该服务存在并且标记为“doctrine.repository_service”。在 (...)\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;
}
}

最佳答案

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

您可以将 CommentRepository 移至实体目录(并相应地更新命名空间),但最好遵循标准结构并将存储库保留在 App\Repository 命名空间中。

关于php - Symfony 4 : entity has a repositoryClass set to "App\Entity\CommentRepository", 但这不是有效的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47858041/

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