在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控制器的这个动作中使用它时:
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”。“ 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;
}
}
答案 0 :(得分:6)
您应该@ORM\Entity(repositoryClass="App\Entity\CommentRepository")
更改为@ORM\Entity(repositoryClass="App\Repository\CommentRepository")
您可以将CommentRepository
移动到Entity目录中(并相应地更新命名空间),但最好遵循标准结构并将您的存储库保存在App \ Repository命名空间中。