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 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。