php - 带有集合一对一关系的嵌入表单的 Symfony 5 错误
问题描述
我正在尝试使用集合类型(一对一关系)嵌入表单,但出现错误:
属性路径“patiPatientsSafeData”中给出的“App\Entity\PatientsSafeData”、“array”类型的预期参数。
在实体 Patient
<?php
namespace App\Entity;
use App\Repository\PatientsRepository;
use Doctrine\ORM\Mapping as ORM;
class Patients
{
private $id;
private $patiLabel;
/**
* @ORM\OneToOne(targetEntity=PatientsSafeData::class, mappedBy="pasaPatient", cascade={"persist", "remove"})
*/
private $patiPatientsSafeData;
和实体PatientsSafeData
<?php
namespace App\Entity;
use App\Repository\PatientsSafeDataRepository;
use Doctrine\ORM\Mapping as ORM;
class PatientsSafeData
{
private $id;
/**
* @ORM\OneToOne(targetEntity=Patients::class, inversedBy="patiPatientsSafeData", cascade={"persist", "remove"})
* @ORM\JoinColumn(nullable=false)
*/
private $pasaPatient;
表单类型
<?php
namespace App\Form;
use App\Form\PatientsSafeDataType;
use App\Entity\Patients;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class PatientsType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('patiLabel',TextType::class, ["label" => "Label"])
->add('patiPatientsSafeData' , CollectionType::class,
[
'entry_type' => PatientsSafeDataType::class,
'entry_options' => ['label' => 'Safe Data'],
'allow_add' => true
])
模板:
<div class="card-body">
<p>Save Data</p>
<p>
<ul class="safeData" id="safeData" data-prototype="{{ form_widget(form.patiPatientsSafeData.vars.prototype)|e('html_attr') }}">
{% for patSafeData in form.patiPatientsSafeData %}
<li>
{{ form_row(patSafeData.pasaName) }}
{{ form_row(patSafeData.pasaSurname) }}
{{ form_row(patSafeData.pasaDOB) }}
</li>
{% endfor %}
</ul>
</p>
</div>
我收到错误保存数据。我不知道我错在哪里,请问有什么想法吗?提前致谢
解决方案
要解决此问题,您必须为 patiPatientSafeData 字段创建自定义表单类型。单击此处查看如何创建自定义 FormType。如果您不想手动创建表单类型,可以使用 symfony-cli 基于 PatientSafeData 实体创建“子”表单,然后将 CollectionType 替换为刚刚创建的新 FormType::class。
您的 PatientType 表单将如下所示:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('patiLabel',TextType::class, ["label" => "Label"])
->add('patiPatientsSafeData' , PatientSafeDataType::class)
;
请注意,我已将 CollectionType 替换为您之前应该使用 cli 创建的新 PatientSafeDataType 表单。
现在您的模板将如下所示
{{ form_start(patientForm) }}
{{ form_row(patientForm.patiLabel) }}
{{ form_row(patientForm.patiPatientsSafeData) }}
<button type="submit">Submit</button>
{{ form_end(patientForm) }}
推荐阅读
- python - 如何打印动态sql约束错误语句
- sql - 如何在 Informix SQL 中创建汇总?
- javascript - SyntaxError:reactjs登录页面上位置0的JSON中的意外令牌r
- android - Android EditText:仅水平滚动
- flutter - 将 CheckBox 中的选中项列表返回到 TabBar 主屏幕 Flutter
- spring - Spring Cloud Stream 多个目标绑定
- javascript - 使用 javascript 选择 ::before 元素或在另一个 div javascript 中创建一个 div
- spring - 如何将重定向查询参数不在 URL 中但隐藏到 Spring Controller 传递
- sql - 如果值不存在,则选择空行
- angular - 如何将http angular网站重定向到代理后面的https