我试图嵌入一个具有集合类型(一对一关系)的表单,但是我得到了错误:
在属性路径"patiPatientsSafeData“处给出的参数类型应为"App\Entity\PatientsSafeData”、"array“。
在实体患者中
<?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;The formType
<?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>保存数据时出现错误。我不知道我哪里错了,求你,你知道吗?提前感谢
发布于 2020-10-20 22:14:04
要解决此问题,您必须为patiPatientSafeData字段创建自定义表单类型。Click here查看如何创建自定义FormType。如果不想手动创建表单类型,可以使用symfony-cli创建一个基于PatientSafeData实体的“子”表单,然后用刚刚创建的新FormType::类替换CollectionType。
您的PatientsType表单将如下所示:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('patiLabel',TextType::class, ["label" => "Label"])
->add('patiPatientsSafeData' , PatientSafeDataType::class)
;请注意,我已经用您之前用cli创建的新PatientSafeDataType表单替换了CollectionType。
现在,您的模板将如下所示
{{ form_start(patientForm) }}
{{ form_row(patientForm.patiLabel) }}
{{ form_row(patientForm.patiPatientsSafeData) }}
<button type="submit">Submit</button>
{{ form_end(patientForm) }}https://stackoverflow.com/questions/64238943
复制相似问题