phpsymfonysymfony4arraycollection

Expected argument of type "App\Entity\Theme", "Doctrine\Common\Collections\ArrayCollection" given at property path "themes"


I have a many to many relationship between Customer, Formation and Theme entities. The insertion form of Customer does not work.

I explain what I want to do : I want to add "Customer" by adding one or more "Formation" and one or more "Themes" knowing that the theme are related to "Formation", ie if a customer has the Training_1, he will not be able to have Theme from Formation_2.

All the code is there.

Customer Entity :

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Form\Form;

/**
 * @ORM\Entity(repositoryClass="App\Repository\FormationRepository")
 * @ORM\Table(name="lol_customer")
 */
class Customer
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=180, unique=true)
     */
    private $customerName;

    /**
     * @var Formation[]|ArrayCollection
     *
     * @ORM\ManyToMany(targetEntity="App\Entity\Formation", cascade={"persist"})
     * @ORM\JoinTable(name="lol_customer_formation")
     * @ORM\OrderBy({"id": "ASC"})
     */
    private $formations;

    /**
     * @var Theme[]|ArrayCollection
     *
     * @ORM\ManyToMany(targetEntity="App\Entity\Theme", cascade={"persist"})
     * @ORM\JoinTable(name="lol_customer_theme")
     * @ORM\OrderBy({"id": "ASC"})
     */
    private $themes;

    /**
     * @ORM\Column(type="boolean")
     */
    private $enabled;

    /**
     * @var \DateTime
     *
     * @ORM\Column(type="datetime")
     */

    private $startDate;
    /**
     * @var \DateTime
     *
     * @ORM\Column(type="datetime")
     */
    private $endDate;

    public function __construct()
    {
        $this->startDate = new \DateTime();
        $this->endDate = new \DateTime();
        $this->formations = new ArrayCollection();
    }



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

    public function setFormations(Formation ...$formations): void
    {
        foreach ($formations as $formation) {
            if (!$this->formations->contains($formation)) {
                $this->formations->add($formation);
            }
        }
    }

    public function getFormations()
    {
        return $this->formations;
    }

    public function setThemes(Theme ...$themes): void
    {
        foreach ($themes as $theme) {
            if (!$this->themes->contains($theme)) {
                $this->themes->add($theme);
            }
        }
    }

    public function getThemes()
    {
        return $this->themes;
    }

    /**
     * @return CustomerName
     */
    public function getCustomerName()
    {
        return $this->customerName;
    }

    public function setCustomerName($customerName): self
    {
        $this->customerName = $customerName;

        return $this;
    }

    /**
     * @return bool
     */
    public function getEnabled(): bool
    {
        return $this->enabled;
    }

    /**
     * @param bool
     * @return self
     */
    public function setEnabled($enabled): self
    {
        $this->enabled = $enabled;

        return $this;
    }

    public function getStartDate(): \DateTime
    {
        return $this->startDate;
    }

    public function setStartDate(\DateTime $startDate): void
    {
        $this->startDate = $startDate;
    }

    public function getEndDate(): \DateTime
    {
        return $this->endDate;
    }

    public function setEndDate(\DateTime $endDate): void
    {
        $this->endDate = $endDate;
    }

    public function __toString()
    {
        return $this->customerName;
    }

}

Formation Entity :

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @ORM\Entity()
 * @ORM\Table(name="lol_formation")
 *
 */
class Formation implements \JsonSerializable
{
    /**
     * @var int
     *
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(type="string", unique=true)
     */
    private $name;

    /**
     * @var Theme[]|ArrayCollection
     *
     * @ORM\ManyToMany(targetEntity="App\Entity\Theme", cascade={"persist"})
     * @ORM\JoinTable(name="lol_formation_theme")
     * @ORM\OrderBy({"name": "ASC"})
     */
    private $themes;

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

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

    public function setName(string $name): void
    {
        $this->name = $name;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    /**
     * {@inheritdoc}
     */
    public function jsonSerialize(): string
    {
        return $this->name;
    }

    public function getThemes(): ?array
    {
        return $this->themes;
    }

    public function setThemes(?array $themes): self
    {
        $this->themes = $themes;

        return $this;
    }

    public function __toString(): string
    {
        return $this->name;
    }
}

Theme Entity :

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity()
 * @ORM\Table(name="lol_Theme")
 *
 */
class Theme implements \JsonSerializable
{
    /**
     * @var int
     *
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(type="string", unique=true)
     */
    private $name;

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

    public function setName(string $name): void
    {
        $this->name = $name;
    }

    public function getName(): ?string
    {
        return $this->name;
    }

    /**
     * {@inheritdoc}
     */
    public function jsonSerialize(): string
    {    
        return $this->name;
    }

    public function __toString(): string
    {
        return $this->name;
    }
}

CustomerType (form of Customer) :

<?php

namespace App\Form;

use App\Entity\Customer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
use App\Form\Type\DateTimePickerType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;

class CustomerType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('customerName', null, array(
                    'label'    => 'Domaine du client ',
                    'required' => true)
            )
            ->add('enabled', null, array(
                'label'    => 'Activer la formation'), [
                'required'   => false,
            ])
            ->add('formations', EntityType::class, [
                'class' => "App\Entity\Formation",
                'choice_label' => 'name',
                'multiple' => true,
                'expanded' => true,
            ])
            ->add('themes', EntityType::class, [
                'class' => "App\Entity\Theme",
                'choice_label' => 'name',
                'multiple' => true,
                'expanded' => true,
            ])
            ->add('startDate', DateTimePickerType::class, [
                'label' => 'Date de début de la formation',
            ])
            ->add('endDate', DateTimePickerType::class, [
                'label' => 'Date de fin de la formation',
            ]);
    }

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

I have the following error :

Expected argument of type "App\Entity\Theme", "Doctrine\Common\Collections\ArrayCollection" given at property path "themes".

Do you know where this error comes from? Thank you


Solution

  • Inside constructor of customer entity you are missing initialization for "themes" array collection.