1

我正在尝试使用 CraueFormFlow 制作多步骤表单。CraueForm 需要一个类来补充水分,但我有几个实体,所以我编辑了一个 addChildWizard 类,其中所有我需要的实体都具有如下属性:

    <?php

    namespace VS\CrmBundle\Entity;

    use Doctrine\Common\Collections\ArrayCollection;
    use VS\CrmBundle\Entity\MedicalRecord;
    use VS\CrmBundle\Entity\Person;
    use VS\CrmBundle\Entity\Relationship;
    use Doctrine\ORM\Mapping as ORM;

    /**
     *
     * Class AddChildWizard
     * @package VS\CrmBundle\Entity
     */
    class AddChildWizard
    {
        /**
         * Step 1
         *
         * @var Relationship
         */
        protected $currenUserChildRelationship;

        /**
         * Step 1
         *
         * @var Person
         */
        protected $child;

        /**
         * Step 2
         *
         * @var MedicalRecord
         */
        protected $childsMedicalRecord;

        /**
         * Step 3
         *
         * This is a collection of Relationship entities
         *
         * @var ArrayCollection
         */
        protected $childsFamily;

        public function __construct()
        {
            $this->childsFamily = new ArrayCollection();
        }
+ getters and setters

然后我有我的流课:

<?php

namespace VS\CrmBundle\Form\Wizard\AddChild;

use Craue\FormFlowBundle\Form\FormFlow;
use Craue\FormFlowBundle\Form\FormFlowInterface;


class AddChildFlow extends FormFlow {
    protected function loadStepsConfig()
    {
        return array(
            array(
                'label' => 'ChildData',
                'form_type' => 'VS\CrmBundle\Form\Wizard\AddChild\AddChildStep1'
            ),
            array(
                'label' => 'ChildMedicalRecord',
                'form_type' => 'VS\CrmBundle\Form\Wizard\AddChild\AddChildStep2'
            ),
            array(
                'label' => 'ChildFamily',
                'form_type' => 'VS\CrmBundle\Form\Wizard\AddChild\AddChildStep3'
            ),
            array(
                'label' => 'confirmation',
            ),
        );
    }
}

每个步骤都有一个表格。例如第 1 步是:

<?php

namespace VS\CrmBundle\Form\Wizard\AddChild;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use VS\CrmBundle\Entity\Person;
use VS\CrmBundle\Entity\Relationship;
use VS\CrmBundle\Form\PersonChildType;
use VS\CrmBundle\Form\RelationshipFromCurrentUserType;

class AddChildStep1 extends AbstractType {
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('currenUserChildRelationship', RelationshipFromCurrentUserType::class, array(
                'data_class' => Relationship::class
            ))
            ->add('child', PersonChildType::class, array(
                'data_class' => Person::class
            ));
    }

    public function getBlockPrefix()
    {
        return 'AddChildStep1';
    }
}

行动是:

 public function addChildAction()
    {

        // Our form data class
        $formData = new AddChildWizard();

        // We call service (craue form flow)
        $flow = $this->get('vs_crm.form.flow.add_child');
        $flow->bind($formData);

        $form = $flow->createForm();



        if($flow->isValid($form))
        {
            $flow->saveCurrentStepData($form);

            if($flow->nextStep())
            {
                $form = $flow->createForm();
            }
            else
            {
                $child = $formData->getChild();
                $curentUserRel = $formData->getCurrenUserChildRelationship();
                $currentUser = $this->get('security.token_storage')->getToken()->getUser();
                $medicalRecord = $formData->getChildsMedicalRecord();
                $family = $formData->getChildsFamily();

                $curentUserRel->setSourceId($child);
                $curentUserRel->setDestinationId($currentUser);
                $medicalRecord->setPerson($child);

                foreach($family as $member)
                {
                    $member->setSourceId($child);
                }

                //return new JsonResponse(array($formData->getId()));

                // flow finished

                $em = $this->getDoctrine()->getManager();
                $em->persist($formData);
                $em->flush();

                $flow->reset();

                $this->addFlash('success', 'Your child has been saved.');
                return $this->redirectToRoute('vs_crm_parent_dashboard');

            }
        }

        return $this->render('VSCrmBundle:Parent:add-child.html.twig', array(
            'form' => $form->createView(),
            'flow' => $flow
        ));
    }

这完美地工作,但最后我有这个错误:

类“VS\CrmBundle\Entity\AddChildWizard”不是有效的实体或映射的超类。

因此,doctrine 想要一个带有 @Entity 注释和标识符的实体,而我们的朋友 Doctrine 想要将标识符保存在数据库中......

有没有其他方法可以在不将 id 保存在数据库中的情况下做这些事情,或者我应该听我们的朋友 Doctrine ?

4

1 回答 1

1

好的,如果它可以帮助某人...

最后我有了$em->persist($formData);,这就是为什么 Doctrine 希望我的类 addChildWizard ( $formData = new AddChildWizard();) 成为一个实体。我所做的是:

 $em->persist($child);
 $em->persist($curentUserRel);
 $em->persist($medicalRecord);

 foreach($family as $familyMember)
 {
     $em->persist($familyMember);
 }

现在这种多步骤形式完美运行!

于 2017-01-20T14:16:18.107 回答