我正在尝试在页面上创建多个相同的表单,以便将产品和成员分配给制造商。在我的Controller中,我动态初始化一个自定义表单对象,以分隔View和Form的数据并存储一些数据。
<?php
namespace App\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\MoneyType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use App\Entity\Manufacturer;
use App\Entity\User;
use App\Entity\Product;
use App\Entity\ProductCategory;
use App\Form\ManufacturerType;
class ManufacturerController extends Controller
{
// Private Methods
/**
* Initialize a Form for each Manufacturer to add a product
*/
private function initManufacturerProductForm($manufacturer)
{
$form = $this->createNamedBuilder()
->add('name', TextType::class, array(
'label' => false,
'attr' => array(
'placeholder' => 'Nom du produit'
)))
->add('category', EntityType::class, array(
'class' => ProductCategory::class,
'choice_label' => 'category',
'label' => false,
'placeholder' => 'Categorie',
'attr' => array(
'class' => 'form-choice'
)))
->add('description', TextType::class, array(
'required' => false,
'label' => false,
'attr' => array(
'placeholder' => 'Description'
)))
->add('dose', NumberType::class, array(
'required' => false,
'label' => false,
'attr' => array(
'placeholder' => 'Dose (en mg)'
)))
->add('price', MoneyType::class, array(
'currency' => '',
'label' => false,
'attr' => array(
'placeholder' => 'Prix'
)))
->add('thumbnail', FileType::class, array(
'label' => false,
'attr' => array(
'placeholder' => 'Image'
)))
->getForm();
return ($form);
}
/**
* Initialize a Form for each Manufacturer to add a Member
*/
private function initManufacturerMemberForm($usersRepo)
{
$form = $this->createFormBuilder()
->add('user', EntityType::class, array(
'class' => User::class,
'choice_label' => 'username',
'label' => false,
'placeholder' => 'Selectionnez un utilisateur',
'attr' => array(
'class' => 'form-choice'
)))
->getForm();
return ($form);
}
/**
* Initialize a Form for each Manufacturer to add a product
*/
private function saveManufacturerProduct($form)
{
$product = new Product();
$product->setName($form['formData']->get('name')->getData());
$product->setCategory($form['formData']->get('category')->getData());
$product->setDescription($form['formData']->get('description')->getData());
$product->setDose($form['formData']->get('dose')->getData());
$product->setPrice($form['formData']->get('price')->getData());
$directory = $this->get('kernel')->getRootDir() . '/../public/media/img/products-thumbnails/';
$file = $form['formData']->get('thumbnail')->getData();
$extension = $file->guessExtension();
$filename = 'thumb' . time() . '.' . $extension;
$file->move($directory, $filename);
$product->setThumbnail($filename);
$manufacturer = $this->getDoctrine()->getRepository(Manufacturer::class)->find($form['manufacturerId']);
$product->setManufacturer($manufacturer);
$em = $this->getDoctrine()->getManager();
$em->persist($product);
$em->flush();
return ($product);
}
/**
* Initialize a Form for each Manufacturer to add a product
*/
private function saveManufacturerMember($form)
{
dump($form);
$em = $this->getDoctrine()->getManager();
$user = $this->getDoctrine()->getRepository(User::class)->find($form['formData']->get('user')->getData()->getId());
$manufacturer = $this->getDoctrine()->getRepository(Manufacturer::class)->find($form['manufacturerId']);
$user->setManufacturer($manufacturer);
$em->flush();
return ($user);
}
// Public Methods
/**
* @Route("/manufacturers", name="manufacturers")
*/
public function index(Request $request)
{
$manufacturer = new Manufacturer();
$form = $this->createForm(ManufacturerType::class, $manufacturer, array(
'is_admin' => $this->isGranted('ROLE_ADMIN'),
));
$manufacturersRepo = $this->getDoctrine()->getRepository(Manufacturer::class);
$manufacturers = $manufacturersRepo->findAll();
$usersRepo = $this->getDoctrine()->getRepository(User::class);
$i = 0;
$productForms = array();
foreach ($manufacturers as $manufacturer) {
$formData = $this->initManufacturerProductForm($manufacturer);
$formView = $formData->createView();
$productForms[$i++] = array(
'formId' => $i,
'manufacturerId' => $manufacturer->getID(),
'formData' => $formData,
'formView' => $formView,
);
}
$i = 0;
$memberForms = array();
foreach ($manufacturers as $manufacturer) {
$formData = $this->initManufacturerMemberForm($usersRepo);
$formView = $formData->createView();
$memberForms[$i++] = array(
'formId' => $i,
'manufacturerId' => $manufacturer->getID(),
'formData' => $formData,
'formView' => $formView
);
}
// Handle Request to create a Manufacturer
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($manufacturer);
$em->flush();
return $this->redirectToRoute('manufacturers');
}
// Handle Request to add a Product to a Manufacturer
foreach ($productForms as $pform)
{
$pform['formData']->handleRequest($request);
if ($pform['formData']->isSubmitted() && $pform['formData']->isValid()) {
$this->saveManufacturerProduct($pform);
return $this->redirectToRoute('manufacturers');
}
}
// Handle Request to add a Product to a Manufacturer
foreach ($memberForms as $mform)
{
$mform['formData']->handleRequest($request);
if ($mform['formData']->isSubmitted() && $mform['formData']->isValid()) {
$this->saveManufacturerMember($mform);
return $this->redirectToRoute('manufacturers');
}
}
return $this->render('manufacturers.html.twig', array(
'manufacturers' => $manufacturers,
'form' => $form->createView(),
'productforms' => $productForms,
'memberforms' => $memberForms,
));
}
}
错误:
当我提交表单来添加产品时,它会将项目保存在数据库中,但制造商始终是相同的:我在数据库中的第一个ID = 4 当我提交表单以向制造商添加成员时,没有任何反应。
我认为错误是当我处理请求并检查条件以查看表单是否有效时。