在属性路径“主题”中给出的类型为“ App \ Entity \ Theme”,“ Doctrine \ Common \ Collections \ ArrayCollection”的预期参数

时间:2019-07-02 14:26:48

标签: php symfony symfony4 arraycollection

我在Customer,Formation和Theme实体之间有很多关系。客户的插入形式无效。

我解释了我想做什么:我想通过添加一个或多个“格式”和一个或多个“主题”来添加“客户”,因为他们知道主题与“格式”相关,即客户是否拥有Training_1,他将无法从队形_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(客户形式):

<?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”   “主题”。

您知道此错误来自何处吗?谢谢

1 个答案:

答案 0 :(得分:1)

在客户实体的构造函数中,您缺少“主题”数组集合的初始化。