php - 在属性路径“主题”处给出的“App\Entity\Theme”、“Doctrine\Common\Collections\ArrayCollection”类型的预期参数
问题描述
我在客户、形成和主题实体之间有多对多的关系。客户的插入表格不起作用。
我解释了我想要做什么:我想通过添加一个或多个“Formation”和一个或多个“Themes”来添加“Customer”,知道主题与“Formation”相关,即如果客户有 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(客户形式):
<?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
]);
}
}
我有以下错误:
在属性路径“themes”处给出的“App\Entity\Theme”、“Doctrine\Common\Collections\ArrayCollection”类型的预期参数。
你知道这个错误来自哪里吗?谢谢
解决方案
在客户实体的构造函数中,您缺少“主题”数组集合的初始化。
推荐阅读
- flutter - MaterialPageRoute 构建器中的未来返回 null
- asp.net-core-mvc - 剃刀页面视图模型类中的 Url.Action 备用
- microcontroller - Stm32 FreeRtos 内部 Flash 链接器脚本
- javascript - 手写或生成打字稿.d.ts?
- java - 在同一线程上等待和通知
- curl - 使用 curl 的 Amadeus Flight API 实现
- python - Django 疯狂的 URL 重写
- laravel - 如何在 Laravel 中获取(解析)这个 JSON?
- javascript - 如何使用虚拟现实 Javascript 库 - Marzipano?
- javascript - Django Chart.js ajax javascript“字符串数据解析错误”