从Symfony2表单存储实体不起作用

时间:2017-11-07 15:31:48

标签: php symfony doctrine symfony-2.1

我有一个疯狂的问题,我不明白。

我关注的代码看起来像这样:

 public function appendAction(Request $request, $pKpPatientid)
{
    if (!$this->isAdmin()) {
        throw new AccessDeniedException();
    }
    $entity = new DataDFu1();
    $entity1 = $this->getDoctrine()
        ->getRepository('DataLiveBundle:DataAPatient')
        ->find($pKpPatientid);

   $appendForm = $this->createAppendForm($pKpPatientid,$entity, $entity1);


    $appendForm->handleRequest($request);
   // if ($appendForm->isValid()) {
        if($appendForm->get('submit')->isClicked()){//Save
           //return $this->redirect($this->generateUrl('dataapatient_sendMessage', array("pKpPatientid" => $pKpPatientid)));
           $entity->setFu1KfPatientid($entity1);
           $this->storeAppendDataDFu1($entity);

     //   }
    }

    return $this->render('DataLiveBundle:DataDFu1:form.html.twig', array(
       // 'entity'     => $entity,
        'form'   => $appendForm->createView(),
        'isNew'=> true,
    ));
}

/**
 * The function createAppendForm
 * Creates a form with the Information from a DataAPatient.
 * @param DataAPatient $pKpPatientid The primary key
 * @return \Symfony\Component\Form\Form
 */
private function createAppendForm($pKpPatientid, $entity, $entity1)
{   

    $form = $this->createForm($this->get('data_livebundle.form.dataapatienttype'), $entity1, array(
    //'action' => $this->generateUrl('dataHome'),
    'method' => 'POST'
   ));

    $form->add('submit', 'submit', array('label' => 'Create Fu1'));
    return $form->add('dFu1', new DataDFu1Type(), array('data'=>$entity));

}

 /**
 * The function storeEditedDataDFu1
 * Persists changes made to an existing DataDFu1 entity to the database
 * @param  DataDFu1 entity
 * @return DataAPatient $pKpPatientid The primary key
 */
public function storeAppendDataDFu1($entity)
{
    $em = $this->getDoctrine()->getManager();
    $session = $this->getRequest()->getSession();

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find DataDFu1 entity.');
    }

    $em->persist($entity);
    $em->flush();
    $session->getFlashBag()->add(
            'notice',
            'Your changes to the DataDFu1 of ID: "'."xyz". '" was saved!'
    );
   // return $entity->getPKpPatientid();
}

我创建了一个表单,其中包含两个带有实体的表单并进行渲染。它会罚款。但是,在想要存储(storeAppendDataDFu1)来自实体的数据并且只是来自该实体的那一刻,entity1会丢失之前在表单中可视化的所有值(只是可视化的)。这意味着此实体1显示的字段将持久存储,数据库中的值为NULL。

即使只为entity1()编写了一个内存函数,entity1如何持久存储错误的值?

我的推测是它与形式的星座有关,因为每当我按提交时,entity1的字段都被设置为NULL。

我希望有人知道这个问题:),我真的找不到解决方案。 *实体和entity1只是连接在一起,因为来自实体的foreig键是entity1的主键,它是oneToOne匹配...

我发现mappingBy也是null?是什么意思?这可能是原因吗?

 oneToOne:
    fu1KfPatientid:
        targetEntity: DataAPatient
        cascade: {  }
        fetch: LAZY
        mappedBy: null
        inversedBy: dFu1
        joinColumns:
            _FU1_kf_PatientID:
                referencedColumnName: __P_kp_PatientID
        orphanRemoval: false

感谢您的反馈...告诉我您是否需要更多信息.Thaanks

2 个答案:

答案 0 :(得分:0)

如果实体是相关的,那么添加适当的映射信息以保持两个实体并不是更好吗?

  

实体1

/**
*@ORM\OneToOne(targetEntity="Entity2", cascade={"persist"})
*
*/
$entity_two_reference;

在形式中,这将是一个Entity2Type,应该像往常一样呈现

当你进行flush()时,它应该可以正常工作。

答案 1 :(得分:0)

现在我发现了这个问题!问题在于我正在使用 Data-Toggle

{#<div class="container">
<div class="row">
    <ul class="nav nav-tabs pull-left">
         <li class="active"><a href="#patientInfo" data-toggle="tab">Patient info</a></li>
         <li ><a href="#partI" data-toggle="tab">Part I</a></li>
         <li ><a href="#partII" data-toggle="tab">Part II</a></li>
         <li ><a href="#partIII" data-toggle="tab">Part III</a></li>
    </ul>
    {#div class="clearfix">tmp. disable#}</div> #}

创建注册布局。我认为这种布局对Simfony2 Forms不起作用。有人体验哪种寄存器布局与Symfony2 Forms一起工作?