参数必须是 ?array 类型,给定字符串,在 Doctrine/annotations/lib/Doctrine/Common/Annotations/DocParser.php Annotations/DocParser.php 中调用

Chl*_*lem 0 symfony doctrine-orm api-platform.com

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

\n

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

\n

什么说法错误讲。我没有指示。

\n

我的代码:

\n

第一个实体:

\n
\nnamespace App\\Entity;\n\nuse ApiPlatform\\Core\\Annotation\\ApiResource;\nuse App\\Repository\\PostRepository;\nuse Doctrine\\ORM\\Mapping as ORM;\nuse Symfony\\Component\\Serializer\\Annotation\\Groups;\nuse Symfony\\Component\\Validator\\Constraints\\Length;\n\n//read:collection pour lire une collection d'article\n//read:item correspond \xc3\xa0 la lecture d'un seul item\n/*ici je souhaite afficher les attributs title et slug quand je demande une collection d'article mais afficher \n    mais afficher en plus le contennu ainsi que la date quand je fais une requete d'un seul article\n*/\n\n/**\n * @ORM\\Entity(repositoryClass=PostRepository::class)\n */\n#[ApiResource(\n    normalizationContext:['groups'=> ['read:collection']],\n    itemOperations:[\n        'get' => [\n            'normalization_context' => ['groups'=> ['read:collection', 'read:item', 'read:Post']],\n        'put' => [\n            'denormalization_context' => ['groups' => ['put:Post']],\n            ],\n        ],\n    ],\n)]\nclass Post\n{\n    /**\n     * @ORM\\Id\n     * @ORM\\GeneratedValue\n     * @ORM\\Column(type="integer")\n     */\n    #[Groups(['read:collection'])]\n    private $id;\n\n    /**\n     * @ORM\\Column(type="string", length=255)\n     */\n    #[\n        Groups(['read:collection','put:Post']),\n        Length(min:5, max:255),\n\n    ]\n    private $title;\n\n    /**\n     * @ORM\\Column(type="string", length=255)\n     */\n    #[\n    Groups(['read:collection','put:Post']),\n    Length(min:5, max:255),\n    ]\n    private $slug;\n\n    /**\n     * @ORM\\Column(type="text")\n     */\n    #[Groups(['read:item','put:Post'])]\n    private $content;\n\n    /**\n     * @ORM\\Column(type="datetime_immutable", nullable=true)\n     */\n    #[Groups(['read:item','put:Post'])]\n    private $uptdatedAt;\n\n    /**\n     * @ORM\\ManyToOne(targetEntity=Category::class, inversedBy="posts", cascade="persiste")\n     */\n    #[Groups(['read:item','write:Post'])\n    ]\n    private $category;\n\n    public function __construct()\n    {\n        $this->createdAt = new \\DateTime();\n        $this->uptdatedAt = new \\DateTime();\n    }\n\n    public function getId(): ?int\n    {\n        return $this->id;\n    }\n\n    public function getTitle(): ?string\n    {\n        return $this->title;\n    }\n\n    public function setTitle(string $title): self\n    {\n        $this->title = $title;\n\n        return $this;\n    }\n\n    public function getSlug(): ?string\n    {\n        return $this->slug;\n    }\n\n    public function setSlug(string $slug): self\n    {\n        $this->slug = $slug;\n\n        return $this;\n    }\n\n    public function getContent(): ?string\n    {\n        return $this->content;\n    }\n\n    public function setContent(string $content): self\n    {\n        $this->content = $content;\n\n        return $this;\n    }\n\n    public function getUptdatedAt()\n    {\n        return $this->uptdatedAt;\n    }\n\n    public function setUptdatedAt(?\\DateTimeImmutable $uptdatedAt): self\n    {\n        $this->uptdatedAt = $uptdatedAt;\n\n        return $this;\n    }\n\n    public function getCategory(): ?category\n    {\n        return $this->category;\n    }\n\n    public function setCategory(?category $category): self\n    {\n        $this->category = $category;\n\n        return $this;\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我的第二个实体:

\n
\nnamespace App\\Entity;\n\nuse ApiPlatform\\Core\\Annotation\\ApiResource;\nuse App\\Repository\\CategoryRepository;\nuse Doctrine\\Common\\Collections\\ArrayCollection;\nuse Doctrine\\Common\\Collections\\Collection;\nuse Doctrine\\ORM\\Mapping as ORM;\nuse Symfony\\Component\\Serializer\\Annotation\\Groups;\n\n/**\n * @ApiResource()\n * @ORM\\Entity(repositoryClass=CategoryRepository::class)\n */\nclass Category\n{\n    /**\n     * @ORM\\Id\n     * @ORM\\GeneratedValue\n     * @ORM\\Column(type="integer")\n     */\n    #[Groups('read:Post')]\n    private $id;\n\n    /**\n     * @ORM\\Column(type="string", length=255)\n     */\n    #[Groups('read:Post','write:Post')]\n    private $name;\n\n    /**\n     * @ORM\\OneToMany(targetEntity=Post::class, mappedBy="category")\n     */\n    private $posts;\n\n    public function __construct()\n    {\n        $this->posts = new ArrayCollection();\n    }\n\n    public function getId(): ?int\n    {\n        return $this->id;\n    }\n\n    public function getName(): ?string\n    {\n        return $this->name;\n    }\n\n    public function setName(string $name): self\n    {\n        $this->name = $name;\n\n        return $this;\n    }\n\n    /**\n     * @return Collection|Post[]\n     */\n    public function getPosts(): Collection\n    {\n        return $this->posts;\n    }\n\n    public function addPost(Post $post): self\n    {\n        if (!$this->posts->contains($post)) {\n            $this->posts[] = $post;\n            $post->setCategory($this);\n        }\n\n        return $this;\n    }\n\n    public function removePost(Post $post): self\n    {\n        if ($this->posts->removeElement($post)) {\n            // set the owning side to null (unless already changed)\n            if ($post->getCategory() === $this) {\n                $post->setCategory(null);\n            }\n        }\n\n        return $this;\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n

Arl*_*Hix 7

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

/**
 * @ORM\ManyToOne(targetEntity=Category::class, inversedBy="posts", cascade="persiste")
 */
Run Code Online (Sandbox Code Playgroud)

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

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