2022-11-28

Return value must be of type Collections array returned Doctrine / Symfony 6

I have 2 order and line entities, with onetoMany / manytoOne relationship

I want to save a command with its lines, except I need to customize things, so I go through a datapersister function persist

If I don't try to save my lines it goes well, but as soon as I want to save the lines I get this message:

"App\Entity\DoRetourEntete::getRetALignes(): the return value must be of type Doctrine\Common\Collections\Collection, array returned

Basically I'm sending it an array while it's waiting for a collection.

But how to send it a collection in json?

In get everything works fine, I have my line detail when I call a command.

Here is my POST:

{
  "nomCommande": "2398",
  "cmdALignes": [
    {
      "ref": "maref1",
      "des1": "designation1",
      "qtt": 2
    },
    {
      "ref": "maref2",
      "des1": "designation2",
      "qtt": 2
    }
 
    
  ]
}

Here is a piece of my entities:

Commande:

/**
     * @ORM\OneToMany(targetEntity=lignes::class, mappedBy="ligneEntete")
     */
    private $cmdALignes;
 
    public function __construct()
    {
        $this->cmdALignes = new ArrayCollection();
    }
 
    /**
     * @return Collection|Post[]
     */
    public function getCmdALignes(): Collection
    {
        return $this->cmdALignes;
    }
 
    public function setCmdALignes(array $cmdALignes): self
    {
        $this->cmdALignes= $cmdALignes;
 
        return $this;
    }

and ligne :

/**
     *
     * @ORM\ManyToOne(targetEntity=Commande::class, inversedBy="cmdALignes")<br>
     */
    private $ligneEntete;
 
    public function __construct()
    {
        $this->ligneEntete= new ArrayCollection();<br>    }
 
    public function getLigneEntete(): ?Commande
    {
        return $this->ligneEntete;<br>    }
 
 
    public function setLigneEntete(?Commande $ligneEntete): self
    {
        $this->ligneEntete= $ligneEntete;
        return $this;
    }

In my data persister :

$commande = new Commande();
$commande->setNomCommande($data->getNomCommande);

and in principle if I want to browse my lines, I do:

foreach ($data->getCmdALignes() as $ligne){ }

I know that I send an array in a collection but how to do? thanks in advance

Guillaume



No comments:

Post a Comment