API 平台"Unable to generate an IRI for an entity"



我已经使用symfony 6创建了一个API,我正在使用API平台进行过滤器。我有12个实体工作得很好,但其中一个给我带来了错误。

这是执行GET请求时的错误:

Unable to generate an IRI for "App\Entity\Titols"."

这是Titools实体:

<?php
namespace AppEntity;
use DoctrineORMMapping as ORM;
use ApiPlatformCoreAnnotationApiFilter;
use ApiPlatformCoreAnnotationApiResource;
use ApiPlatformCoreBridgeDoctrineOrmFilterSearchFilter;
/**
* Titols
*
* @ORMTable(name="obres")
* @ORMEntity
*/
#[ApiResource]
#[ApiFilter(SearchFilter::class, properties: ['titol' => 'partial'])]
class Titols implements JsonSerializable
{
/**
* @var string
*
* @ORMColumn(name="codi", type="string", length=18, nullable=false)
* @ORMId
* @ORMGeneratedValue(strategy="IDENTITY")
*/
private $codi;
/**
* @var string
*
* @ORMColumn(name="titol", type="string", length=75, nullable=false)
*/
private $titol;
/**
* @var int
*
* @ORMColumn(name="autor_id", type="integer", nullable=false)
*/
private $autorId = '0';
/**
* @var string
*
* @ORMColumn(name="tipus", type="string", length=10, nullable=false)
*/
private $tipus;
/**
* @var string|null
*
* @ORMColumn(name="data", type="string", length=5, nullable=true, options={"default"="NULL"})
*/
private $data = 'NULL';
/**
* @var string|null
*
* @ORMColumn(name="instrument", type="string", length=75, nullable=true, options={"default"="NULL"})
*/
private $instrument = 'NULL';
/**
* @var string|null
*
* @ORMColumn(name="dacomp", type="text", length=65535, nullable=true, options={"default"="NULL"})
*/
private $dacomp = 'NULL';
/**
* @var string|null
*
* @ORMColumn(name="enreg", type="text", length=65535, nullable=true, options={"default"="NULL"})
*/
private $enreg = 'NULL';
/**
* @var string|null
*
* @ORMColumn(name="partitu", type="string", length=17, nullable=true, options={"default"="NULL"})
*/
private $partitu = 'NULL';
/**
* @var string|null
*
* @ORMColumn(name="partice", type="string", length=17, nullable=true, options={"default"="NULL"})
*/
private $partice = 'NULL';
/**
* @var string|null
*
* @ORMColumn(name="pdf1", type="string", length=50, nullable=true, options={"default"="NULL"})
*/
private $pdf1 = 'NULL';
/**
* @var string|null
*
* @ORMColumn(name="pdf2", type="string", length=50, nullable=true, options={"default"="NULL"})
*/
private $pdf2 = 'NULL';
/**
* @var string|null
*
* @ORMColumn(name="pdf3", type="string", length=50, nullable=true, options={"default"="NULL"})
*/
private $pdf3 = 'NULL';
/**
* @var string|null
*
* @ORMColumn(name="notes1", type="string", length=255, nullable=true, options={"default"="NULL"})
*/
private $notes1 = 'NULL';
/**
* @var string|null
*
* @ORMColumn(name="notes2", type="string", length=255, nullable=true, options={"default"="NULL"})
*/
private $notes2 = 'NULL';
/**
* @var string|null
*
* @ORMColumn(name="mp3", type="string", length=50, nullable=true, options={"default"="NULL"})
*/
private $mp3 = 'NULL';
/**
* @var string|null
*
* @ORMColumn(name="uri_spotify", type="string", length=255, nullable=true, options={"default"="NULL"})
*/
private $uriSpotify = 'NULL';
/**
* @var string|null
*
* @ORMColumn(name="notes3", type="string", length=255, nullable=true, options={"default"="NULL"})
*/
private $notes3 = 'NULL';
/**
* @var string|null
*
* @ORMColumn(name="notes4", type="string", length=255, nullable=true, options={"default"="NULL"})
*/
private $notes4 = 'NULL';
/**
* @var int|null
*
* @ORMColumn(name="arranjador_id", type="integer", nullable=true, options={"default"="NULL"})
*/
private $arranjadorId = NULL;

public function getId(): ?string
{
return $this->codi;
}
public function setId(string $codi): self
{
$this->codi = $codi;
return $this;
}
public function getCodi(): ?string
{
return $this->codi;
}
public function setCodi(string $codi): self
{
$this->codi = $codi;
return $this;
}
public function getTitol(): ?string
{
return $this->titol;
}
public function setTitol(string $titol): self
{
$this->titol = $titol;
return $this;
}
public function getAutorId(): ?int
{
return $this->autorId;
}
public function setAutorId(int $autorId): self
{
$this->autorId = $autorId;
return $this;
}
public function getTipus(): ?string
{
return $this->tipus;
}
public function setTipus(string $tipus): self
{
$this->tipus = $tipus;
return $this;
}
public function getData(): ?string
{
return $this->data;
}
public function setData(?string $data): self
{
$this->data = $data;
return $this;
}
public function getInstrument(): ?string
{
return $this->instrument;
}
public function setInstrument(?string $instrument): self
{
$this->instrument = $instrument;
return $this;
}
public function getDacomp(): ?string
{
return $this->dacomp;
}
public function setDacomp(?string $dacomp): self
{
$this->dacomp = $dacomp;
return $this;
}
public function getEnreg(): ?string
{
return $this->enreg;
}
public function setEnreg(?string $enreg): self
{
$this->enreg = $enreg;
return $this;
}
public function getPartitu(): ?string
{
return $this->partitu;
}
public function setPartitu(?string $partitu): self
{
$this->partitu = $partitu;
return $this;
}
public function getPartice(): ?string
{
return $this->partice;
}
public function setPartice(?string $partice): self
{
$this->partice = $partice;
return $this;
}
public function getPdf1(): ?string
{
return $this->pdf1;
}
public function setPdf1(?string $pdf1): self
{
$this->pdf1 = $pdf1;
return $this;
}
public function getPdf2(): ?string
{
return $this->pdf2;
}
public function setPdf2(?string $pdf2): self
{
$this->pdf2 = $pdf2;
return $this;
}
public function getPdf3(): ?string
{
return $this->pdf3;
}
public function setPdf3(?string $pdf3): self
{
$this->pdf3 = $pdf3;
return $this;
}
public function getNotes1(): ?string
{
return $this->notes1;
}
public function setNotes1(?string $notes1): self
{
$this->notes1 = $notes1;
return $this;
}
public function getNotes2(): ?string
{
return $this->notes2;
}
public function setNotes2(?string $notes2): self
{
$this->notes2 = $notes2;
return $this;
}
public function getMp3(): ?string
{
return $this->mp3;
}
public function setMp3(?string $mp3): self
{
$this->mp3 = $mp3;
return $this;
}
public function getUriSpotify(): ?string
{
return $this->uriSpotify;
}
public function setUriSpotify(?string $uriSpotify): self
{
$this->uriSpotify = $uriSpotify;
return $this;
}
public function getNotes3(): ?string
{
return $this->notes3;
}
public function setNotes3(?string $notes3): self
{
$this->notes3 = $notes3;
return $this;
}
public function getNotes4(): ?string
{
return $this->notes4;
}
public function setNotes4(?string $notes4): self
{
$this->notes4 = $notes4;
return $this;
}
public function getArranjadorId(): ?int
{
return $this->arranjadorId;
}
public function setArranjadorId(?int $arranjadorId): self
{
$this->arranjadorId = $arranjadorId;
return $this;
}
public function jsonSerialize()
{
return [
"codi" => $this->getCodi(),
"titol" => $this->getTitol(),
"autor_id" => $this->getAutorId(),
"tipus" => $this->getTipus(),
"data" => $this->getData(),
"instrument" => $this->getInstrument(),
"dacomp" => $this->getDacomp(),
"enreg" => $this->getEnreg(),
"partitu" => $this->getPartitu(),
"partice" => $this->getPartice(),
"pdf1" => $this->getPdf1(),
"pdf2" => $this->getPdf2(),
"pdf3" => $this->getPdf3(),
"notes1" => $this->getNotes1(),
"notes2" => $this->getNotes2(),
"mp3" => $this->getMp3(),
"uri_spotify" => $this->getUriSpotify(),
"notes3" => $this->getNotes3(),
"notes4" => $this->getNotes4(),
"arranjador_id" => $this->getArranjadorId()
];
}
}

这是我拥有的唯一一个id不是数字而是字符串的实体,这可能是问题吗?

这是TitolsController.php中的getAll函数,用于选择数据:

/**
* @Route("/api/titols", methods={"GET"})
*/
public function getAll(TitolsRepository $titolsRepository): Response
{
$this->denyAccessUnlessGranted(TitolsVoter::VIEW);
$titols = $titolsRepository->findBy([], ['codi' => 'ASC'], 10);
return $this->json($titols);
}

我最终没有使用我拥有的自定义控制器,我添加了GET request,正如api平台在文档中所说:

#[ApiResource(
attributes: ["security" => "is_granted('PERMISSION_TITOL')"],
collectionOperations: [
'get' => ['method' => 'get'],
],
itemOperations: [
'get' => [
'path' => '/api/titols'            
],
],
)]

这解决了错误Unable to generate an IRI for "App\Entity\Titols"."

相关内容

最新更新