Symfony 形式 - 类型的预期参数,"string" 在 属性 路径中给出
Symfony forms - Expected argument of type , "string" given at property path
我收到此错误消息:
在 属性 路径 "artist".
处给出的 "App\Entity\Artist or null"、"string" 类型的预期参数
这是我在 symfony 中的表单代码
$builder
->add('date', DateType::class, [
'widget' => 'single_text'
])
->add('artist', TextType::class)
->add('City',TextType::class)
;
这是模板:
{{ form_start(form)}}
{{ form_widget(form)}}
<button class="btn btn-secondary">{{ button|default('Enregistrer')}}</button>
{{ form_end(form)}}
实体
class Artist
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=45)
*/
private $name;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Event", mappedBy="artist", cascade={"persist","remove"})
*/
private $events;
public function __construct()
{
$this->events = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): self
{
$this->name = $name;
return $this;
}
/**
* @return Collection|Event[]
*/
public function getEvents(): Collection
{
return $this->events;
}
public function addEvent(Event $event): self
{
if (!$this->events->contains($event)) {
$this->events[] = $event;
$event->setArtist($this);
}
return $this;
}
public function removeEvent(Event $event): self
{
if ($this->events->contains($event)) {
$this->events->removeElement($event);
// set the owning side to null (unless already changed)
if ($event->getArtist() === $this) {
$event->setArtist(null);
}
}
return $this;
}
public function __toString()
{
return $this->name;
}
}
这是错误:
Uncaught PHP Exception Symfony\Component\PropertyAccess\Exception\InvalidArgumentException: "Expected argument of type "App\Entity\Artist or null", "string" given at property path "artist"." at D:\wamp64\www\app_music_events\vendor\symfony\property-access\PropertyAccessor.php line 173
{
"exception": {}
}
这是控制器
/**
* Method for editing an event
*
* @Route("/admin/edit/{id}", name="admin.edit", methods="GET|POST")
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function edit(Event $event, Request $request): Response
{
$form = $this->createForm(EventType::class, $event);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->em->flush();
$this->addFlash('success', 'Le concert a été modifié avec succès !');
return $this->redirectToRoute('admin.index');
}
return $this->render('admin/edit.html.twig', [
'event' => $event,
'form' => $form->createView()
]
);
}
我不明白。
当我没有在表单中指定 Textype 时,我没有这个问题。
问题是什么?谢谢
问题是您需要将艺术家实体与您的事件一起持久化。 Symfony 需要一个 Artist 实体类型而不是一个字符串。尝试使用 EntityType::class
而不是 TextType::class
->add('artist', EntityType::class, [
'class' => Artist::class
]);
有关 EntityType 字段的更多信息,请check the documentation
我收到此错误消息: 在 属性 路径 "artist".
处给出的 "App\Entity\Artist or null"、"string" 类型的预期参数这是我在 symfony 中的表单代码
$builder
->add('date', DateType::class, [
'widget' => 'single_text'
])
->add('artist', TextType::class)
->add('City',TextType::class)
;
这是模板:
{{ form_start(form)}}
{{ form_widget(form)}}
<button class="btn btn-secondary">{{ button|default('Enregistrer')}}</button>
{{ form_end(form)}}
实体
class Artist
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=45)
*/
private $name;
/**
* @ORM\OneToMany(targetEntity="App\Entity\Event", mappedBy="artist", cascade={"persist","remove"})
*/
private $events;
public function __construct()
{
$this->events = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function setName(?string $name): self
{
$this->name = $name;
return $this;
}
/**
* @return Collection|Event[]
*/
public function getEvents(): Collection
{
return $this->events;
}
public function addEvent(Event $event): self
{
if (!$this->events->contains($event)) {
$this->events[] = $event;
$event->setArtist($this);
}
return $this;
}
public function removeEvent(Event $event): self
{
if ($this->events->contains($event)) {
$this->events->removeElement($event);
// set the owning side to null (unless already changed)
if ($event->getArtist() === $this) {
$event->setArtist(null);
}
}
return $this;
}
public function __toString()
{
return $this->name;
}
}
这是错误:
Uncaught PHP Exception Symfony\Component\PropertyAccess\Exception\InvalidArgumentException: "Expected argument of type "App\Entity\Artist or null", "string" given at property path "artist"." at D:\wamp64\www\app_music_events\vendor\symfony\property-access\PropertyAccessor.php line 173
{
"exception": {}
}
这是控制器
/**
* Method for editing an event
*
* @Route("/admin/edit/{id}", name="admin.edit", methods="GET|POST")
* @param Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
public function edit(Event $event, Request $request): Response
{
$form = $this->createForm(EventType::class, $event);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$this->em->flush();
$this->addFlash('success', 'Le concert a été modifié avec succès !');
return $this->redirectToRoute('admin.index');
}
return $this->render('admin/edit.html.twig', [
'event' => $event,
'form' => $form->createView()
]
);
}
我不明白。 当我没有在表单中指定 Textype 时,我没有这个问题。
问题是什么?谢谢
问题是您需要将艺术家实体与您的事件一起持久化。 Symfony 需要一个 Artist 实体类型而不是一个字符串。尝试使用 EntityType::class
而不是 TextType::class
->add('artist', EntityType::class, [
'class' => Artist::class
]);
有关 EntityType 字段的更多信息,请check the documentation