Symfony d'affiche cet erreur 500
Expected argument of type "?App\Entity\Ingredient", "array" given at property path "Ingredient".
J'arrive pas d'entrer les données dans la base de donnée avec un aide de formulaire dans easyadmin.

RecetteCrudController

<?php

namespace App\Controller\Admin;

use App\Entity\Recette;
use App\Form\Type\IngredientsType;
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Field\CollectionField;
use EasyCorp\Bundle\EasyAdminBundle\Field\FormField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IdField;
use EasyCorp\Bundle\EasyAdminBundle\Field\IntegerField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextareaField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextEditorField;
use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;

class RecetteCrudController extends AbstractCrudController
{
    public static function getEntityFqcn(): string
    {
        return Recette::class;
    }

    public function configureFields(string $pageName): iterable
    {
        return [

            IdField::new('id')->hideOnForm(),
            TextField::new('nomRecette')->setHelp('Veuillez entrer le nom du plat'),
            AssociationField::new('nomCategorie')->setHelp('Veuillez choisir une catégorie'),
            CollectionField::new('Ingredient')->setEntryType(IngredientsType::class),
            TextField::new('pays')->setHelp('Veuillez entrer le pays d\'origine du plat'),
            TextField::new('tempsDePreparation')->setHelp('Temps de préparation, par ex. 30 min'),
            TextField::new('tempsDeCuisson')->setHelp('Temps de cuisson, par ex. 1h30'),
            TextField::new('difficulte')->setHelp('Niveau de difficulté : difficile, moyen, facile'),
            TextareaField::new('preparation')->setHelp('Détails de la préparation'),

        ];
    }

}

IngredientsType.php


<?php

namespace App\Form\Type;

use App\Entity\Ingredient;
use App\Form\EventListener\AddNameFieldSubscriber;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class IngredientsType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder

        ->add('nomIngredient', TextType::class, [
            //placeholder
            'help' => 'farine, lait',
        ])
        ->add('quantite', IntegerType::class, [
            'help' => 'mettez un chiffre',
        ])
        ->add('mesure', TextType::class, [
            'help' => 'kilogramme, gramme, cuillère à soupe, cuillère à café, millilitre, litre',
        ]);

    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => Ingredient::class,
        ]);
    }
}

Entité Recette

<?php

namespace App\Entity;
use App\Entity\Ingredient;
use App\Repository\RecetteRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: RecetteRepository::class)]
class Recette
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255)]
    private ?string $nomRecette = null;

    #[ORM\ManyToOne(inversedBy: 'recettes', targetEntity: Ingredient::class, cascade:['persist'])]
    private ?Ingredient $Ingredient = null;

    #[ORM\Column(type: Types::TEXT)]
    private ?string $preparation = null;

    #[ORM\ManyToOne(inversedBy: 'nomRecettes')]
    private ?Categorie $nomCategorie = null;

    #[ORM\Column(length: 20)]
    private ?string $tempsDeCuisson = null;

    #[ORM\Column(length: 30)]
    private ?string $tempsDePreparation = null;

    #[ORM\Column(length: 20)]
    private ?string $difficulte = null;

    #[ORM\Column(length: 100)]
    private ?string $pays = null;

    /**
     * @var Collection<int, NotePlat>
     */
    #[ORM\OneToMany(targetEntity: NotePlat::class, mappedBy: 'recette')]
    private Collection $notePlats;

    #[ORM\ManyToOne(inversedBy: 'recette')]
    private ?Commentaire $commentaires = null;

    public function __construct()
    {
        $this->notePlats = new ArrayCollection();
    }

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

    public function getNomRecette(): ?string
    {
        return $this->nomRecette;
    }

    public function setNomRecette(string $nomRecette): static
    {
        $this->nomRecette = $nomRecette;

        return $this;
    }

    public function getIngredient(): ?Ingredient
    {
        return $this->Ingredient;
    }

    public function setIngredient(?Ingredient $Ingredient): static
    {
        $this->Ingredient = $Ingredient;

        return $this;
    }

    public function getPreparation(): ?string
    {
        return $this->preparation;
    }

    public function setPreparation(string $preparation): static
    {
        $this->preparation = $preparation;

        return $this;
    }

    public function getNomCategorie(): ?Categorie
    {
        return $this->nomCategorie;
    }

    public function setNomCategorie(?Categorie $nomCategorie): static
    {
        $this->nomCategorie = $nomCategorie;

        return $this;
    }

    public function getTempsDeCuisson(): ?string
    {
        return $this->tempsDeCuisson;
    }

    public function setTempsDeCuisson(string $tempsDeCuisson): static
    {
        $this->tempsDeCuisson = $tempsDeCuisson;

        return $this;
    }

    public function getTempsDePreparation(): ?string
    {
        return $this->tempsDePreparation;
    }

    public function setTempsDePreparation(string $tempsDePreparation): static
    {
        $this->tempsDePreparation = $tempsDePreparation;

        return $this;
    }

    public function getDifficulte(): ?string
    {
        return $this->difficulte;
    }

    public function setDifficulte(string $difficulte): static
    {
        $this->difficulte = $difficulte;

        return $this;
    }

    public function getPays(): ?string
    {
        return $this->pays;
    }

    public function setPays(string $pays): static
    {
        $this->pays = $pays;

        return $this;
    }

    /**
     * @return Collection<int, NotePlat>
     */
    public function getNotePlats(): Collection
    {
        return $this->notePlats;
    }

    public function addNotePlat(NotePlat $notePlat): static
    {
        if (!$this->notePlats->contains($notePlat)) {
            $this->notePlats->add($notePlat);
            $notePlat->setRecette($this);
        }

        return $this;
    }

    public function removeNotePlat(NotePlat $notePlat): static
    {
        if ($this->notePlats->removeElement($notePlat)) {
            // set the owning side to null (unless already changed)
            if ($notePlat->getRecette() === $this) {
                $notePlat->setRecette(null);
            }
        }

        return $this;
    }

    public function getCommentaires(): ?Commentaire
    {
        return $this->commentaires;
    }

    public function setCommentaires(?Commentaire $commentaires): static
    {
        $this->commentaires = $commentaires;

        return $this;
    }
}

Entité Ingredient

<?php

namespace App\Entity;

use App\Repository\IngredientRepository;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;

#[ORM\Entity(repositoryClass: IngredientRepository::class)]
class Ingredient
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 100)]
    private ?string $nomIngredient = null;

    #[ORM\Column]
    private ?int $quantite = null;

    #[ORM\Column(length: 30)]
    private ?string $mesure = null;

    /**
     * @var Collection<int, Recette>
     */
    #[ORM\OneToMany(targetEntity: Recette::class, mappedBy: 'Ingredient')]
    private Collection $recettes;

    public function __construct()
    {
        $this->recettes = new ArrayCollection();
    }

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

    public function getNomIngredient(): ?string
    {
        return $this->nomIngredient;
    }

    public function setNomIngredient(string $nomIngredient): static
    {
        $this->nomIngredient = $nomIngredient;

        return $this;
    }

    public function getQuantite(): ?int
    {
        return $this->quantite;
    }

    public function setQuantite(int $quantite): static
    {
        $this->quantite = $quantite;

        return $this;
    }

    public function getMesure(): ?string
    {
        return $this->mesure;
    }

    public function setMesure(string $mesure): static
    {
        $this->mesure = $mesure;

        return $this;
    }

    /**
     * @return Collection<int, Recette>
     */
    public function getRecettes(): Collection
    {
        return $this->recettes;
    }

    public function addRecette(Recette $recette): static
    {
        if (!$this->recettes->contains($recette)) {
            $this->recettes->add($recette);
            $recette->setIngredient($this);
        }

        return $this;
    }

    public function removeRecette(Recette $recette): static
    {
        if ($this->recettes->removeElement($recette)) {
            // set the owning side to null (unless already changed)
            if ($recette->getIngredient() === $this) {
                $recette->setIngredient(null);
            }
        }

        return $this;
    }
}

Ce que je veux

Je voudrais remplire l'entité Recette et aussi Ingredient en cascade, pour que ça se rempli au même temps

Ce que j'obtiens

Expected argument of type "?App\Entity\Ingredient", "array" given at property path "Ingredient".

Aucune réponse