参数必须是类型 ?array, string given, called in doctrine/annotations/lib/Doctrine/Common/Annotations/DocParser.php Annotations/DocParser.php

Argument must be of type ?array, string given, called in doctrine/annotations/lib/Doctrine/Common/Annotations/DocParser.php Annotations/DocParser.php

我不明白我在 API 平台上的错误。

Doctrine\ORM\Mapping\ManyToOne::__construct(): 参数 #2 ($cascade) 必须是类型 ?array, string given, called in /Users/charleslem/symfony/API-plateform-php-8/Api-plateform-php8-symfony5/vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/DocParser.php在第 944 行。

什么参数错误再说。我没有指示。

我的代码:

第一个实体:


namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\PostRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints\Length;

//read:collection pour lire une collection d'article
//read:item correspond à la lecture d'un seul item
/*ici je souhaite afficher les attributs title et slug quand je demande une collection d'article mais afficher 
    mais afficher en plus le contennu ainsi que la date quand je fais une requete d'un seul article
*/

/**
 * @ORM\Entity(repositoryClass=PostRepository::class)
 */
#[ApiResource(
    normalizationContext:['groups'=> ['read:collection']],
    itemOperations:[
        'get' => [
            'normalization_context' => ['groups'=> ['read:collection', 'read:item', 'read:Post']],
        'put' => [
            'denormalization_context' => ['groups' => ['put:Post']],
            ],
        ],
    ],
)]
class Post
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    #[Groups(['read:collection'])]
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    #[
        Groups(['read:collection','put:Post']),
        Length(min:5, max:255),

    ]
    private $title;

    /**
     * @ORM\Column(type="string", length=255)
     */
    #[
    Groups(['read:collection','put:Post']),
    Length(min:5, max:255),
    ]
    private $slug;

    /**
     * @ORM\Column(type="text")
     */
    #[Groups(['read:item','put:Post'])]
    private $content;

    /**
     * @ORM\Column(type="datetime_immutable", nullable=true)
     */
    #[Groups(['read:item','put:Post'])]
    private $uptdatedAt;

    /**
     * @ORM\ManyToOne(targetEntity=Category::class, inversedBy="posts", cascade="persiste")
     */
    #[Groups(['read:item','write:Post'])
    ]
    private $category;

    public function __construct()
    {
        $this->createdAt = new \DateTime();
        $this->uptdatedAt = new \DateTime();
    }

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getTitle(): ?string
    {
        return $this->title;
    }

    public function setTitle(string $title): self
    {
        $this->title = $title;

        return $this;
    }

    public function getSlug(): ?string
    {
        return $this->slug;
    }

    public function setSlug(string $slug): self
    {
        $this->slug = $slug;

        return $this;
    }

    public function getContent(): ?string
    {
        return $this->content;
    }

    public function setContent(string $content): self
    {
        $this->content = $content;

        return $this;
    }

    public function getUptdatedAt()
    {
        return $this->uptdatedAt;
    }

    public function setUptdatedAt(?\DateTimeImmutable $uptdatedAt): self
    {
        $this->uptdatedAt = $uptdatedAt;

        return $this;
    }

    public function getCategory(): ?category
    {
        return $this->category;
    }

    public function setCategory(?category $category): self
    {
        $this->category = $category;

        return $this;
    }
}

我的第二个实体:


namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use App\Repository\CategoryRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ApiResource()
 * @ORM\Entity(repositoryClass=CategoryRepository::class)
 */
class Category
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    #[Groups('read:Post')]
    private $id;

    /**
     * @ORM\Column(type="string", length=255)
     */
    #[Groups('read:Post','write:Post')]
    private $name;

    /**
     * @ORM\OneToMany(targetEntity=Post::class, mappedBy="category")
     */
    private $posts;

    public function __construct()
    {
        $this->posts = 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;
    }

    /**
     * @return Collection|Post[]
     */
    public function getPosts(): Collection
    {
        return $this->posts;
    }

    public function addPost(Post $post): self
    {
        if (!$this->posts->contains($post)) {
            $this->posts[] = $post;
            $post->setCategory($this);
        }

        return $this;
    }

    public function removePost(Post $post): self
    {
        if ($this->posts->removeElement($post)) {
            // set the owning side to null (unless already changed)
            if ($post->getCategory() === $this) {
                $post->setCategory(null);
            }
        }

        return $this;
    }
}

在您的注释中,您定义了一个 ManyToOne 关系并尝试将 cascade 选项设置为一个字符串值:

/**
 * @ORM\ManyToOne(targetEntity=Category::class, inversedBy="posts", cascade="persiste")
 */

正如错误明确指出的那样,它应该是一个数组:

/**
 * @ORM\ManyToOne(targetEntity=Category::class, inversedBy="posts", cascade={"persist"})
 */