getOrderProducts() as $orderProduct) { //dump($orderProduct); $orderProduct->setCreatedBy($this->getUser()); $orderProduct->setUpdatedBy($this->getUser()); $orderProduct->setTaxRate($orderProduct->getProduct()->getProductFamily()->getTaxRate()); $orderProduct->setOrderShop($entity); $this->em->persist($orderProduct); $entity->addOrderProduct($orderProduct); } $this->setUpdated($entity); parent::updateEntity($entity); } public function persistEntity($entity) { foreach ($entity->getOrderProducts() as $orderProduct) { $orderProduct->setUnit($orderProduct->getProduct()->getUnitInherited()); $orderProduct->setTitle($orderProduct->getProduct()->getTitleInherited()); $orderProduct->setPrice($orderProduct->getProduct()->getPriceInherited()); if ($orderProduct->getProduct()->getProductFamily()->getTaxRate()) { $orderProduct->setTaxRate($orderProduct->getProduct()->getProductFamily()->getTaxRate()); } else { $orderProduct->setTaxRate($this->security->getUser()->getMerchant()->getTaxRate()); } $orderProduct->setOrderShop($entity); $orderProduct->setCreatedBy($this->getUser()); $orderProduct->setUpdatedBy($this->getUser()); $this->em->persist($orderProduct); $entity->addOrderProduct($orderProduct); } parent::persistEntity($entity); } public function createEntityFormBuilder($entity, $view, $override=true) { $formBuilder = parent::createEntityFormBuilder($entity, $view); $formBuilder->add('orderProducts', CollectionType::class, array( 'label' => 'Déclinaisons', 'entry_type' => OrderProductType::class, 'entry_options' => ['label' => false], 'allow_add' => true, 'allow_delete' => true, 'required' => true ) ); $userClass = $this->em->getClassMetadata(UserInterface::class); $formBuilder->add('user', EntityType::class, array( 'class' => $userClass->name )); $addressClass = $this->em->getClassMetadata(AddressInterface::class); $formBuilder->add('invoiceAddress', EntityType::class, array( 'class' => $addressClass->name, 'placeholder' => '', 'query_builder' => function (EntityRepository $er) use ($entity) { return $er->createQueryBuilder('a') ->where('a.user = :user') ->setParameter('user', $entity->getUser()); }, )); return $formBuilder; } public function getUserViaFirstStepForm($entity) { $userClass = $this->em->getClassMetadata(UserInterface::class); $userChoiceForm = $this->createFormBuilder($entity) ->add('user', EntityType::class, array( 'class' => $userClass->name )) ->add('nextStep', SubmitType::class) ->getForm(); $userChoiceForm->handleRequest($this->request); if ($userChoiceForm->isSubmitted() && $userChoiceForm->isValid()) { return $userChoiceForm->get('user')->getData(); } $parameters = [ 'form' => $userChoiceForm->createView(), 'formView' => 'default', 'entity' => $entity, ]; return $this->executeDynamicMethod('renderTemplate', ['new', $this->entity['templates']['new'], $parameters]); } protected function newAction() { $this->dispatch(EasyAdminEvents::PRE_NEW); $entity = $this->executeDynamicMethod('createNewEntity'); $easyadmin = $this->request->attributes->get('easyadmin'); $easyadmin['item'] = $entity; $this->request->attributes->set('easyadmin', $easyadmin); $fields = $this->entity['new']['fields']; $newForm = $this->executeDynamicMethod('createNewForm', [$entity, $fields]); $newForm->handleRequest($this->request); //ETAPE 1 Choix de l'utilisateur $user = $newForm->get('user')->getData(); if ($user == null) { $user = $this->getUserViaFirstStepForm($entity); } if (!$user instanceof UserInterface) return $user; else { $entity->setUser($user); //réinitialise le formulaire pour charger les adresses en fonction de l'utilisateur sélectionné $newForm = $this->executeDynamicMethod('createNewForm', [$entity, $fields]); $newForm->handleRequest($this->request); //dump( $entity); //dump( $entity); if ($newForm->isSubmitted() && $newForm->isValid()) { /*dump($entity); die();*/ //$this->dispatch(EasyAdminEvents::PRE_PERSIST, ['entity' => $entity]); $this->executeDynamicMethod('persistEntity', [$entity, $newForm]); $this->dispatch(EasyAdminEvents::POST_PERSIST, ['entity' => $entity]); return $this->redirectToReferrer(); } $this->dispatch(EasyAdminEvents::POST_NEW, [ 'entity_fields' => $fields, 'form' => $newForm, 'entity' => $entity ]); $parameters = [ 'form' => $newForm->createView(), 'entity_fields' => $fields, 'entity' => $entity ]; return $this->executeDynamicMethod('renderTemplate', ['new', $this->entity['templates']['new'], $parameters]); } } }