我使用 DQL 进行了自定义查询,但对象看不到该方法



当我学习Symfony时,我在DQL中的自定义查询中遇到了问题,有一个指向文档的链接:https://symfony.com/doc/current/doctrine.html#querying-with-dql-or-sql 我做了同样的函数,就像这样:

<?php
namespace AppRepository;
use AppEntityCatPost;
use DoctrineBundleDoctrineBundleRepositoryServiceEntityRepository;
use SymfonyBridgeDoctrineRegistryInterface;
/**
* @method CatPost|null find($id, $lockMode = null, $lockVersion = null)
* @method CatPost|null findOneBy(array $criteria, array $orderBy = null)
* @method CatPost[]    findAll()
* @method CatPost[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class CatPostRepository extends ServiceEntityRepository
{
public function __construct(RegistryInterface $registry)
{
parent::__construct($registry, CatPost::class);
}
public function findAllCategoriesConnectedToPost($id) :array
{
$entityManager = $this->getEntityManager();
$query = $entityManager->createQuery(
'SELECT cat.id_cat 
FROM AppEntityCatPost cat 
WHERE cat.id_post = :idPost'
);
$query->setParameter('idPost', $id);
return $query->execute();
}
}

实体类 :

<?php
namespace AppEntity;
use DoctrineORMMapping as ORM;
/**
* @ORMEntity(repositoryClass="AppRepositoryCatPostRepository")
*/
class CatPost
{
/**
* @ORMId()
* @ORMGeneratedValue()
* @ORMColumn(type="integer")
*/
private $id;
/**
* @ORMColumn(type="integer")
*/
private $id_post;
/**
* @ORMColumn(type="integer")
*/
private $id_cat;
public function getId()
{
return $this->id;
}
public function getIdPost(): ?int
{
return $this->id_post;
}
public function setIdPost(int $id_post): self
{
$this->id_post = $id_post;
return $this;
}
public function getIdCat(): ?int
{
return $this->id_cat;
}
public function setIdCat(int $id_cat): self
{
$this->id_cat = $id_cat;
return $this;
}
}

然后我想在我的控制器中使用它,如下所示:

/App/Controller/ArticleController
$categories = $this->getDoctrine()
->getRepository(CatPost::class)
->findAllCategoriesConnectedToPost($id);
var_dump($categories);
die();

结果,我得到了空数组,PhpStorm告诉我"找不到方法" 你有什么想法如何解决它吗?

您是否确保链接到实体中的存储库?

/**
* CatPost
*
* @ORMEntity(repositoryClass="AppRepositoryCatPostRepository")
*/
class CatPost

您可以在更上方的同一文档页面上看到这一点 - https://symfony.com/doc/current/doctrine.html#creating-an-entity-class

最新更新