dtdb99743 2017-01-10 13:02
浏览 36
已采纳

持久/刷新表单会在数据库中创建一个额外的行

I have a form with multiple rows created from one table (no relationships to other tables). When I save the form, every change I've made is saved, but I do have an additional empty row in the database. See below for (hopefully) all neccessary informations.

PropertyAdditionCostFrequency.php

/**
 * @ORM\Entity
 * @ORM\Table(name="property_addition_cost_frequency")
 */
class PropertyAdditionCostFrequency
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", nullable=true)
     */
    private $label = '';

    private $costFrequencyCollection;

    public function __construct()
    {
        $this->costFrequencyCollection    = new ArrayCollection();
    }
    // + the getters and setters
}

PropertyAdditionCostFrequencyLabelType.php

class PropertyAdditionCostFrequencyLabelType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('label' )
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => PropertyAdditionCostFrequency::class,
        ));
    }
}

PropertyAdditionCostFrequencyForm.php

class PropertyAdditionCostFrequencyForm extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add(  'cost_frequency_collection', CollectionType::class, array(
                'entry_type' => PropertyAdditionCostFrequencyLabelType::class,
                'label' => ''
            ))
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => PropertyAdditionCostFrequency::class
        ]);
    }
}

AdminCoreDataController.php

public function showCoreDataListAction( Request $request )
{
    $PropertyAdditionCostFrequency = new PropertyAdditionCostFrequency();

    $repository = $this->getDoctrine()->getRepository('AppBundle:PropertyAdditionCostFrequency');
    $cost_frequency = $repository->findAll();

    foreach ($cost_frequency as $k => $v) {
        $PropertyAdditionCostFrequency->getCostFrequencyCollection()->add($v);
    }

    $form = $this->createForm( PropertyAdditionCostFrequencyForm::class, $PropertyAdditionCostFrequency);
    $form->handleRequest($request);

    if($form->isSubmitted() && $form->isValid()){
        $propertyAdditionCostFrequency_form  = $form->getData();
        $em = $this->getDoctrine()->getManager();
        $em->persist($propertyAdditionCostFrequency_form);
        $em->flush();

        $this->addFlash('success', 'successfully changed the data');
        return $this->redirectToRoute('admin_core_data');
    }

    return $this->render('logged_in/content/admin/core_data/core_data.html.twig', [
        'propertyCostFrequencyForm' => $form->createView()
    ]);
}

core_data.html.twig

{{ form_start(propertyCostFrequencyForm) }}

<div class="row" id="cost_frequency_box">
    {% for single_frequency in propertyCostFrequencyForm.cost_frequency_collection %}
        <div class="row js-single-cost-frequency-box">
            <div class="col-sm-1 js-delete-cost-frequency">
                <a href="/admin/property/delete/5">
                    <i class="fa fa-trash"></i>
                </a>
            </div>
            <div class="col-sm-4">
                {{ form_widget(single_frequency.label) }}
            </div>
        </div>
    {% endfor %}
</div>
<div class="row">
    <div class="col-sm-3">
        <button class="col-sm-12 btn btn-success" type="submit">Speichern</button>
    </div>
</div>

{{ form_end(propertyCostFrequencyForm) }}

a dump() of $propertyAdditionCostFrequency_form right before '$em->persist($propertyAdditionCostFrequency_form)'

PropertyAdditionCostFrequency {#394 ▼
    -id: null
    -label: ""
    -costFrequencyCollection: ArrayCollection {#395 ▼
        -elements: array:4 [▼
      0 => PropertyAdditionCostFrequency {#422 ▼
            -id: 1
            -label: "1"
            -costFrequencyCollection: null
      }
      1 => PropertyAdditionCostFrequency {#424 ▼
            -id: 2
            -label: "2"
            -costFrequencyCollection: null
      }
      2 => PropertyAdditionCostFrequency {#425 ▼
            -id: 47
            -label: "3"
            -costFrequencyCollection: null
      }
      3 => PropertyAdditionCostFrequency {#426 ▼
            -id: 38
            -label: "4"
            -costFrequencyCollection: null
      }
    ]
  }
}
  • 写回答

1条回答 默认 最新

  • doukou1718 2017-01-11 07:33
    关注

    The reason why this is happening is evident from the dump you have posted: the top level PropertyAdditionCostFrequency has the id set to null, so of course it is going to create a new row for that, so if you were simply wondering why it creates a new row thats your reason :).

    On the other hand I can't help but think, you have misunderstood how symfony forms / doctrine works, UNLESS having multiple child PropertyAdditionCostFrequency added to one parent PropertyAdditionCostFrequency is the desired effect.

    IF it is not the desired effect and you simply want to create a form which contains a form for every entry of PropertyAdditionCostFrequency in the database(which I would not recommend, because once the DB gets big, it won't be very efficient), you can just use createFormBuilder method inside the controller, add an arbitrary CollectionType field (i.e. $builder->add('frequencies', CollectionType::class), then just create an array and assign the result of findAll() to a key(key name should correspond to the field name, in this case, 'frequencies') and finally set that as the data for the form you have just created (use $builder->getForm() method to get the form, since thats what you want in the controller and you should use the set data method on that :)) (P.S. these are just rough guidelines, I'm not sure, that I haven't missed anything)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗