Ожидаемый аргумент типа «App \ Entity \ Theme», «Doctrine \ Common \ Collections \ ArrayCollection», заданный в пути свойства «themes» - PullRequest
1 голос
/ 02 июля 2019

У меня есть много-много отношений между клиентами, формациями и темамиФорма ввода Клиента не работает.

Я объясняю, что я хочу сделать: я хочу добавить «Клиента», добавив одну или несколько «Формирований» и одну или несколько «Тем», зная, что тема относится к «Формированию», т. Е. Еслиу клиента есть Training_1, он не сможет получить тему от Formation_2.

Весь код есть.

Клиентский объект:

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

}

Образовательный объект:

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

Тема субъекта:

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 (форма 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
        ]);
    }
}

У меня следующая ошибка:

Ожидаемый аргумент типа «App \ Entity \ Theme», «Doctrine \ Common \ Collections \ ArrayCollection», заданный в пути свойства «themes».

Знаете ли вы, откуда возникает эта ошибка?Спасибо

1 Ответ

1 голос
/ 03 июля 2019

Внутри конструктора объекта клиента отсутствует инициализация для коллекции массивов "themes".

...