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

API PLATFORM "Unable to generate an IRI for an entity"

我已经使用 symfony 6 创建了一个 API,并且我正在使用 api 平台进行过滤。我有 12 个实体运行良好,但有一个向我抛出错误。

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

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

这是 Titols 实体:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;

/**
 * Titols
 *
 * @ORM\Table(name="obres")
 * @ORM\Entity
 */
#[ApiResource]
#[ApiFilter(SearchFilter::class, properties: ['titol' => 'partial'])]
class Titols implements \JsonSerializable
{
    /**
     * @var string
     *
     * @ORM\Column(name="codi", type="string", length=18, nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $codi;

    /**
     * @var string
     *
     * @ORM\Column(name="titol", type="string", length=75, nullable=false)
     */
    private $titol;

    /**
     * @var int
     *
     * @ORM\Column(name="autor_id", type="integer", nullable=false)
     */
    private $autorId = '0';

    /**
     * @var string
     *
     * @ORM\Column(name="tipus", type="string", length=10, nullable=false)
     */
    private $tipus;

    /**
     * @var string|null
     *
     * @ORM\Column(name="data", type="string", length=5, nullable=true, options={"default"="NULL"})
     */
    private $data = 'NULL';

    /**
     * @var string|null
     *
     * @ORM\Column(name="instrument", type="string", length=75, nullable=true, options={"default"="NULL"})
     */
    private $instrument = 'NULL';

    /**
     * @var string|null
     *
     * @ORM\Column(name="dacomp", type="text", length=65535, nullable=true, options={"default"="NULL"})
     */
    private $dacomp = 'NULL';

    /**
     * @var string|null
     *
     * @ORM\Column(name="enreg", type="text", length=65535, nullable=true, options={"default"="NULL"})
     */
    private $enreg = 'NULL';

    /**
     * @var string|null
     *
     * @ORM\Column(name="partitu", type="string", length=17, nullable=true, options={"default"="NULL"})
     */
    private $partitu = 'NULL';

    /**
     * @var string|null
     *
     * @ORM\Column(name="partice", type="string", length=17, nullable=true, options={"default"="NULL"})
     */
    private $partice = 'NULL';

    /**
     * @var string|null
     *
     * @ORM\Column(name="pdf1", type="string", length=50, nullable=true, options={"default"="NULL"})
     */
    private $pdf1 = 'NULL';

    /**
     * @var string|null
     *
     * @ORM\Column(name="pdf2", type="string", length=50, nullable=true, options={"default"="NULL"})
     */
    private $pdf2 = 'NULL';

    /**
     * @var string|null
     *
     * @ORM\Column(name="pdf3", type="string", length=50, nullable=true, options={"default"="NULL"})
     */
    private $pdf3 = 'NULL';

    /**
     * @var string|null
     *
     * @ORM\Column(name="notes1", type="string", length=255, nullable=true, options={"default"="NULL"})
     */
    private $notes1 = 'NULL';

    /**
     * @var string|null
     *
     * @ORM\Column(name="notes2", type="string", length=255, nullable=true, options={"default"="NULL"})
     */
    private $notes2 = 'NULL';

    /**
     * @var string|null
     *
     * @ORM\Column(name="mp3", type="string", length=50, nullable=true, options={"default"="NULL"})
     */
    private $mp3 = 'NULL';

    /**
     * @var string|null
     *
     * @ORM\Column(name="uri_spotify", type="string", length=255, nullable=true, options={"default"="NULL"})
     */
    private $uriSpotify = 'NULL';

    /**
     * @var string|null
     *
     * @ORM\Column(name="notes3", type="string", length=255, nullable=true, options={"default"="NULL"})
     */
    private $notes3 = 'NULL';

    /**
     * @var string|null
     *
     * @ORM\Column(name="notes4", type="string", length=255, nullable=true, options={"default"="NULL"})
     */
    private $notes4 = 'NULL';

    /**
     * @var int|null
     *
     * @ORM\Column(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\"."