Symfony4:实体将repositoryClass设置为“App Entity CommentRepository”,但这不是有效的类
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Symfony4:实体将repositoryClass设置为“App Entity CommentRepository”,但这不是有效的类相关的知识,希望对你有一定的参考价值。
在Symfony4中我有一个存储库:
<?php
namespace AppRepository;
use AppEntityComment;
use DoctrineBundleDoctrineBundleRepositoryServiceEntityRepository;
use SymfonyBridgeDoctrineRegistryInterface;
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
无效。
为什么这提到qazxsw poi而不是qazxsw poi?
任何的想法 ?
编辑:
这是评论实体:
AppEntityCommentRepository
你应该AppRepositoryCommentRepository
change到<?php
namespace AppEntity;
use DoctrineORMMapping as ORM;
/**
* MemberBundleEntityComment
*
* @ORMTable(name="comment")
* @ORMEntity(repositoryClass="AppEntityCommentRepository")
*/
class Comment
{
/**
* @var integer $id
*
* @ORMColumn(name="id", type="integer")
* @ORMId
* @ORMGeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var AppEntityNews $news
*
* @ORMManyToOne(targetEntity="AppEntityNews")
* @ORMJoinColumn(name="news", referencedColumnName="id", onDelete="CASCADE")
*/
private $news;
/**
* @var AppEntityUser $author
*
* @ORMManyToOne(targetEntity="AppEntityUser")
* @ORMJoinColumn(name="author", referencedColumnName="id", onDelete="CASCADE")
*/
private $author;
/**
* @var text $content
*
* @ORMColumn(name="content", type="text")
*/
private $content;
/**
* @var datetime $date
*
* @ORMColumn(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 AppEntityNews $news
*/
public function setNews($news)
{
$this->news = $news;
}
/**
* Get news
*
* @return AppEntityNews
*/
public function getNews()
{
return $this->news;
}
/**
* Set author
*
* @param AppEntityUser $author
*/
public function setAuthor($author)
{
$this->author = $author;
}
/**
* Get author
*
* @return AppEntityUser
*/
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;
}
}
否则,您可以将@ORMEntity(repositoryClass="AppEntityCommentRepository")
in移动到Entity目录(并相应地更新命名空间),但最好遵循标准结构并将您的存储库保存在App Repository命名空间中。
以上是关于Symfony4:实体将repositoryClass设置为“App Entity CommentRepository”,但这不是有效的类的主要内容,如果未能解决你的问题,请参考以下文章