PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, January 11, 2022

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

 January 11, 2022     api-platform.com, doctrine-orm, symfony     No comments   

Issue

i don't understand my error on API Plateform.

Doctrine\ORM\Mapping\ManyToOne::__construct(): Argument #2 ($cascade) must be of type ?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 on line 944.

What argument error talk. i don't have indication.

That my code:

First entity:


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;
    }
}

And my second entity :


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;
    }
}

Solution

In your annotations you define a ManyToOne relation and attempt to set the cascade option to a string value:

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

As the error clearly states, it should be an array:

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


Answered By - Arleigh Hix
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing