Bonjour,
J'essaye de créer un formulaire avec plusieurs étapes et j'ai été conseillé sur ce forum à utiliser Craue Form Bundle
Tout marche magnifiquement bien sauf à l'étape où je sauvegarde mes entités ... En gros j'ai :
1) une classe "form data class" telle que :
J'ai mon flow qui est :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51 <?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(); }
Un exemple de formulaire avec le formulaire pour l'étape 1
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 <?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', ), ); } }
et enfin mon action :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 <?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'; } }
Tout est merveilleux jusqu'à ce que j'arrive à la dernière étape et que j'appuie sur Sauvegarder ! Doctrine me dit :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60 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 )); }
et si je met les anotations @Entity Doctrine m'obllige à mettre un identifiant et à en faire une table de ma classe La table contient juste un identifiant que notre ami Doctrine veut à tout prix.Class "VS\CrmBundle\Entity\AddChildWizard" is not a valid entity or mapped super class.
La question est : y a-t-il une autre manière de faire ou dois-je écouter céder aux caprices de Doctrine et le laisser faire sa table avec une seule colonne étant l'identifiant ?
Ok j'ai trouvé d'ou vient l'erreur. Au lieu de faire $em->persist($formData) il faut faire persist sur chaque entité contenue dans le formData.
Partager