我使用 PHP - Symfony 5。
我想在 API 平台上返回一个带有 graphQL 的集合,但我有一个问题:
"debugMessage": "Service "App\DataProvider\CompaniesCollectionDataProvider" not found: the container inside "Symfony\Component\DependencyInjection\Argument\ServiceLocator" is a smaller service locator that only knows about the "App\Resolver\CheckRecruiterQueryResolver", "App\Resolver\ForgotRecruiterQueryResolver" and "App\Resolver\JobBoard\FindOffersByCompanyQueryResolver" services.",
首先,我创建了一个解析器,但查看文档后,我意识到有必要创建一个数据提供程序。
不幸的是,这并没有解决问题,我仍然有相同的错误消息......
我的自定义数据提供程序:
namespace AppDataProvider;
use ApiPlatformCoreDataProviderCollectionDataProviderInterface;
use AppEntityCandidate;
use AppEntityCompany;
use DoctrinePersistenceManagerRegistry;
class CompaniesCollectionDataProvider implements CollectionDataProviderInterface
{
private $doctrine;
public function __construct(ManagerRegistry $doctrine)
{
$this->doctrine = $doctrine;
}
public function getCollection
(string $resourceClass, string $operationName = null, array $context = [])
{
$candidateId = $context['filters']['candidate_id'];
$candidate = $this->doctrine->getRepository(Candidate::class)
->findOneBy(['id' => $candidateId]);
if(empty($candidate->getPosition()))
{
$companies = $this->doctrine->getRepository(Company::class)
->findByStoreIsNational();
// return companies where stores related = is_national
// getCollection doit retourner un array
return $companies;
}
$companies = $this->doctrine->getRepository(Company::class)
->findByStoreIsNational();
return $companies;
}
}
我的实体 :
<?php
namespace AppEntity;
use ApiPlatformCoreAnnotationApiResource;
use AppDataProviderCompaniesCollectionDataProvider;
use DoctrineCommonCollectionsArrayCollection;
use DoctrineCommonCollectionsCollection;
use DoctrineORMMapping as ORM;
use GedmoMappingAnnotation as Gedmo;
use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;
/**
* @ApiResource(graphql={
* "search"={
* "collection_query"=CompaniesCollectionDataProvider::class,
* "args"={
* "candidate_id"={"type"="Int!", "description"="Candidate_id"}
* }
* },
* "create", "update", "delete", "collection_query", "item_query"
* })
*
* @GedmoSoftDeleteable(fieldName="deletedAt", timeAware=false)
* @ORMEntity(repositoryClass="AppRepositoryCompanyRepository")
* @ORMTable(name="companies")
* @UniqueEntity("name")
*/
class Company
{
/**
* @ORMId()
* @ORMGeneratedValue()
* @ORMColumn(type="integer")
*/
private $id;
/**
* @ORMColumn(type="string", length=255)
*/
private $name;
/**
* @ORMOneToMany(targetEntity="AppEntityMedia", mappedBy="company")
*/
private $logo;
/**
* @ORMColumn(type="datetime", nullable=true)
*/
private $createdAt;
/**
* @ORMColumn(type="datetime", nullable=true)
*/
private $updatedAt;
/**
* @ORMColumn(type="datetime", nullable=true)
*/
private $deletedAt;
/**
* @ORMOneToMany(targetEntity="AppEntityStore", mappedBy="company")
*/
private $stores;
public function __construct()
{
$this->stores = new ArrayCollection();
$this->stories = new ArrayCollection();
$this->logo = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function getLogo(): ?string
{
return $this->logo[0];
}
public function setLogo(string $logo): self
{
$this->logo = $logo;
return $this;
}
public function getCreatedAt(): ?DateTimeInterface
{
return $this->createdAt;
}
public function setCreatedAt(DateTimeInterface $createdAt): self
{
$this->createdAt = $createdAt;
return $this;
}
public function getUpdatedAt(): ?DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
public function getDeletedAt(): ?DateTimeInterface
{
return $this->deletedAt;
}
public function setDeletedAt(?DateTimeInterface $deletedAt): self
{
$this->deletedAt = $deletedAt;
return $this;
}
/**
* @return Collection|Store[]
*/
public function getStores(): Collection
{
return $this->stores;
}
public function addStore(Store $store): self
{
if (!$this->stores->contains($store)) {
$this->stores[] = $store;
$store->setCompany($this);
}
return $this;
}
public function removeStore(Store $store): self
{
if ($this->stores->contains($store)) {
$this->stores->removeElement($store);
// set the owning side to null (unless already changed)
if ($store->getCompany() === $this) {
$store->setCompany(null);
}
}
return $this;
}
public function addLogo(Media $logo): self
{
if (!$this->logo->contains($logo)) {
$this->logo[] = $logo;
$logo->setCompany($this);
}
return $this;
}
public function removeLogo(Media $logo): self
{
if ($this->logo->contains($logo)) {
$this->logo->removeElement($logo);
// set the owning side to null (unless already changed)
if ($logo->getCompany() === $this) {
$logo->setCompany(null);
}
}
return $this;
}
}
我的存储库 :
class CompanyRepository extends AbstractRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, Company::class);
}
public function findByStoreIsNational() {
return $this->createQueryBuilder('c')
->leftJoin('c.stores', 's')
->where('s.isNational = true')
->getQuery()
->getResult();
}
我提前感谢您的帮助,
问候
罗曼
如自定义查询的文档中所述,您需要创建自定义解析程序,而不是自定义数据提供程序。
CompaniesCollectionDataProvider
应该变得CompanyCollectionResolver
,应该实施QueryCollectionResolverInterface
。