【问题标题】:How do I return more than one row from an entity to a form in Symfony 3如何在 Symfony 3 中将多个实体从实体返回到表单
【发布时间】:2017-07-27 18:42:38
【问题描述】:

我正在尝试将多个行从实体返回到表单。表单和控制器如下。

表格

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class ExceptionFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('usrid')
            ->add('trcode')
            ->add('cola', NumberType::class,
                ['scale' => 2])
            ->add('colb', NumberType::class,
                ['scale' => 2]
            )
            ->add('colc', NumberType::class,
                ['scale' => 2])
            ->add('cold')
            ->add('fringe', ChoiceType::class,
                array(
                    'choices' => array(
                        'No' => 0,
                        'Yes' => 1
                    )
                ));

    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => 'AppBundle\Entity\Exception'
        ]);
    }

    public function getBlockPrefix()
    {
        return 'app_bundle_exception_form_type';
    }
}

这是我的控制器

public function editExceptions(Request $request, Exception $exception)
    {
        $form = $this->createForm(ExceptionFormType::class, $exception);

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $exceptions = $form->getData();

            $em = $this->getDoctrine()->getManager();
            $em->persist($exceptions);
            $em->flush();

            $this->addFlash('success', 'Exception Added');

            return $this->redirectToRoute('home');
        }

        return $this->render('exceptions/edit.html.twig', [
            'exceptionForm' => $form->createView()
        ]);
    }

这可以工作并呈现一行的所有信息。有时我会有不止一行需要在表单中呈现。当我查看调试时,我得到了这个 sql。

SELECT 
  t0.usrid AS usrid_1, 
  t0.trcode AS trcode_2, 
  t0.cola AS cola_3, 
  t0.colb AS colb_4, 
  t0.colc AS colc_5, 
  t0.cold AS cold_6, 
  t0.dept AS dept_7, 
  t0.fringe AS fringe_8, 
  t0.seq AS seq_9, 
  t0.id AS id_10 
FROM 
  exception t0 
WHERE 
  t0.usrid = ? 
LIMIT 
  1

如何删除“LIMIT 1”? 我不确定我问的是正确的问题,正确的方式。我只使用 Symfony 大约一个月。我搜索了 google、stackoverflow 和 Symfony 文档,但找不到我要查找的内容或如何准确地询问我要查找的内容。

我会尝试更好地解释自己。

|---------|---------|---------|--------|---------|---------|---------|
| usrid   | trcode  | cola    | colb   | colc    | cold    | fringe  |
|---------|---------|---------|--------|---------|---------|---------|
| 25      |   A     | bill    | smith  | 2.00    | 4.00    | yes     |
|---------|---------|---------|--------|---------|---------|---------|
| 25      |   C     | bill    | smith  | 4.34    | 5.00    | yes     |
|---------|---------|---------|--------|---------|---------|---------|
| 25      |   F     | bill    | smith  | 1.54    | 2.76    | no      |
|---------|---------|---------|--------|---------|---------|---------|
| 38      |   A     | bob     | smith  | 2.00    | 4.00    | yes     |
|---------|---------|---------|--------|---------|---------|---------|
| 56      |   L     | maggie  | smith  | 2.00    | 4.00    | yes     |
|---------|---------|---------|--------|---------|---------|---------|
| 21      |   G     | mark    | smith  | 2.00    | 4.00    | yes     |
|---------|---------|---------|--------|---------|---------|---------|

这就是我的桌子的样子。在表单上,​​我需要显示前 3 行,因为它们是相同的 usrid。我知道如何显示其他行,因为它非常简单。这仅使用一个实体。我没有加入或使用任何存储库。

【问题讨论】:

  • 你想说什么?你想得到很多实体?并重复表格 n 次?
  • 是的。我可以有 5 个相同的 usrid,但不同的 cola、colb、colc、cold 和 fringe 数据。
  • 嗯,一个实体的一种形式,但你想要可乐、可乐等的集合......?你可以添加你的实体吗?因为我不明白你想要什么

标签: forms symfony symfony-forms


【解决方案1】:

如果我理解您的问题,您需要每行一个表单 (ExceptionFormType),即使他们共享他们的用户 ID。如果您可以(根据您的评论)有 5 行具有相同的 usid,这意味着您将需要 5 个表单,但它们都可以嵌入到父表单中。

看起来该实体属于另一个实体,其中每个 usrid 只有一行(我们称之为用户)。

所以基本上,你想要的是创建一个包含 ExceptionFormType 表单的表单。

看看: https://symfony.com/doc/current/form/form_collections.html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-26
    • 2016-03-21
    • 1970-01-01
    • 1970-01-01
    • 2015-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多