/** * @param ObjectManager $manager * @param $batteryType * @param $name * @param $count */ protected function abbBattery(ObjectManager $manager, $batteryType, $name, $count) { $battery = new Battery(); $battery->setBatteryType($batteryType); $battery->setName($name); $battery->setCount($count); $manager->persist($battery); $manager->flush(); }
/** * @Route("/new") * @Method({"POST"}) */ public function postAction(Request $request) { $form = $this->initForm($request, BatteryType::class); if ($form->isValid()) { $em = $this->getDoctrine()->getManager(); //todo: form itself is able to generate battery, so no need to create it manually in this case $battery = new Battery(); //todo: if you configure form correctly, then $form->getData() will return Battery object with all parameters set. $battery->setName($form->get('name')->getData()); $battery->setBatteryType($form->get('batteryType')->getData()); $battery->setCount($form->get('count')->getData()); $em->persist($battery); $em->flush(); return $this->redirectToRoute('app_batterypack_report'); } //todo: this is really not needed. Form is able to render errors togather with fields. In flash bag there should be just a message that "you have errors" $this->saveErrorsToFlashBag($request, $form); return $this->redirectToRoute('app_batterypack_index'); }