处理同类型表格
Handling forms of the same type
我需要在一个页面上创建和处理同一实体的几种形式。另外,我需要为它们中的每一个设置单独的提交按钮。
现在,我在一个循环中为每个实体创建了一个表单数组,但是当我尝试处理它时 - 它崩溃了。
主要问题是,当我提交表单时,一切似乎都很好 - 但产品总是第一个生成的。
转储表单给出了结果,它们与正确的产品实体相关联,但在处理后它崩溃了。
我的代码片段:
控制器:
foreach ($products as $product) {
$counter = 0;
$date = $comparativeDate;
$tempCart = new Cart();
$tempCart->setProduct($product['product']);
$tempCart->setUserId($user);
$tempCart->setQuantity(0);
$tempCartForm = $this->createForm('AppBundle\Form\CartType',$tempCart, array('kind' => 'myProducts'));
$tempCartForm->handleRequest($request);
if($tempCartForm->isSubmitted() && $tempCartForm->isValid())
{
$data = $tempCartForm->getData();
$query = $em->createQueryBuilder()
->select('c','p')
->from('AppBundle:Cart','c')
->join('c.product','p')
->where("p = :product")
->andWhere("c.userId = :user")
->setParameter('product',$product['product'])
->setParameter('user',$user->getId())
->getQuery();
$foundCart = $query->getResult();
if(is_null($foundCart) || empty($foundCart))
{
$em->persist($data);
}else{
$foundCart = $foundCart[0];
$foundCart->setQuantity($foundCart->getQuantity()+$data->getQuantity());
$em->persist($foundCart);
}
$em->flush();
return $this->redirectToRoute('my_products');
}
foreach ($products as $secondProduct) {
if($product['product'] === $secondProduct['product'])
{
$counter++;
if($date<$secondProduct['date'])
{
$date = $secondProduct['date'];
}
unset($secondProduct);
}
$productId = $product['product']->getId();
//szykujemy tablice z danymi gotowymi do wyswietlenia
$groupedProducts[$productId]['count']=$counter;
$groupedProducts[$productId]['product']=$product['product'];
$groupedProducts[$productId]['last_date']=$date;
$groupedProducts[$productId]['form_view']=$tempCartForm->createView();
}
}
购物车类型:
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\OptionsResolver\OptionsResolver;
class CartType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('quantity', IntegerType::class, [
'label' => false
]);
if($options['kind']=='myProducts'){
$builder->add('save', SubmitType::class, [
'label' => 'SLIJ'
])
->add('product', EntityType::class, [
'class' => 'AppBundle:Product',
]);
}
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Cart',
'kind' => null
));
}
}
?>
购物车字段:
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="carts")
*/
private $userId;
/**
* @ORM\ManyToOne(targetEntity="Product", inversedBy="carts")
*/
protected $product;
/**
* @ORM\Column(type="integer")
*/
private $quantity;
表格是在模板中为每个 groupedProducts 行生成的。
The main problem is, that when I'm submitting the form, everything seems fine - but the product is ALWAYS the first generated one.
如果您提交表单,应执行以下代码
if($tempCartForm->isSubmitted() && $tempCartForm->isValid())
{
// code ...
return $this->redirectToRoute('my_products');
}
那么它不会到达处理第二个产品的代码,是吗?
我需要在一个页面上创建和处理同一实体的几种形式。另外,我需要为它们中的每一个设置单独的提交按钮。
现在,我在一个循环中为每个实体创建了一个表单数组,但是当我尝试处理它时 - 它崩溃了。
主要问题是,当我提交表单时,一切似乎都很好 - 但产品总是第一个生成的。
转储表单给出了结果,它们与正确的产品实体相关联,但在处理后它崩溃了。
我的代码片段:
控制器:
foreach ($products as $product) {
$counter = 0;
$date = $comparativeDate;
$tempCart = new Cart();
$tempCart->setProduct($product['product']);
$tempCart->setUserId($user);
$tempCart->setQuantity(0);
$tempCartForm = $this->createForm('AppBundle\Form\CartType',$tempCart, array('kind' => 'myProducts'));
$tempCartForm->handleRequest($request);
if($tempCartForm->isSubmitted() && $tempCartForm->isValid())
{
$data = $tempCartForm->getData();
$query = $em->createQueryBuilder()
->select('c','p')
->from('AppBundle:Cart','c')
->join('c.product','p')
->where("p = :product")
->andWhere("c.userId = :user")
->setParameter('product',$product['product'])
->setParameter('user',$user->getId())
->getQuery();
$foundCart = $query->getResult();
if(is_null($foundCart) || empty($foundCart))
{
$em->persist($data);
}else{
$foundCart = $foundCart[0];
$foundCart->setQuantity($foundCart->getQuantity()+$data->getQuantity());
$em->persist($foundCart);
}
$em->flush();
return $this->redirectToRoute('my_products');
}
foreach ($products as $secondProduct) {
if($product['product'] === $secondProduct['product'])
{
$counter++;
if($date<$secondProduct['date'])
{
$date = $secondProduct['date'];
}
unset($secondProduct);
}
$productId = $product['product']->getId();
//szykujemy tablice z danymi gotowymi do wyswietlenia
$groupedProducts[$productId]['count']=$counter;
$groupedProducts[$productId]['product']=$product['product'];
$groupedProducts[$productId]['last_date']=$date;
$groupedProducts[$productId]['form_view']=$tempCartForm->createView();
}
}
购物车类型:
namespace AppBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\OptionsResolver\OptionsResolver;
class CartType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('quantity', IntegerType::class, [
'label' => false
]);
if($options['kind']=='myProducts'){
$builder->add('save', SubmitType::class, [
'label' => 'SLIJ'
])
->add('product', EntityType::class, [
'class' => 'AppBundle:Product',
]);
}
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Cart',
'kind' => null
));
}
}
?>
购物车字段:
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="carts")
*/
private $userId;
/**
* @ORM\ManyToOne(targetEntity="Product", inversedBy="carts")
*/
protected $product;
/**
* @ORM\Column(type="integer")
*/
private $quantity;
表格是在模板中为每个 groupedProducts 行生成的。
The main problem is, that when I'm submitting the form, everything seems fine - but the product is ALWAYS the first generated one.
如果您提交表单,应执行以下代码
if($tempCartForm->isSubmitted() && $tempCartForm->isValid())
{
// code ...
return $this->redirectToRoute('my_products');
}
那么它不会到达处理第二个产品的代码,是吗?