简体   繁体   English

在持久化实体之前处理表单数据-Symfony 2.6

[英]Manipulate form data before persisting entity - Symfony 2.6

I have an entity which has a field called end_date which is datetime. 我有一个实体,该实体具有一个名为end_date的字段,该字段为datetime。 In my symfony form I have a unbinded field which you can select an amount of days eg 3 days, 5days, 7 days. 在我的symfony表单中,我有一个未绑定的字段,您可以选择一个天数,例如3天,5天,7天。

What i need to do is manipulate the amount of days provided by the form (which i don't want to go into the db) and do the calculations of when the end _date would be in relation to the amount of days selected and then persist the end_date. 我需要做的是操纵表单提供的天数(我不想进入数据库),并计算_date结束时间与所选天数有关,然后持续结束日期。

What im having trouble with is how to do the data manipulation in between creating the form and persisting the entities to the db. 我遇到的麻烦是如何在创建表单和将实体持久保存到数据库之间进行数据操作。

Here is my functionality for the form as it stands: 这是表格的当前功能:

public function saveNewListing($request, $controller){
        $listing = new Listing();
        $product = new Product();
        $listing->setProduct($product);
        $product->setUser($controller->getUser());
        $form = $controller->createForm(new SellType(), $auction)->handleRequest($request);

        if($form->isValid()) {
            $em = $controller->getDoctrine()->getManager();
            $em->persist($listing);
            $em->persist($product);
            $em->flush();
            return true;
        }
        return $form;
    }

So inbetween binding the form data and the entities and checking if the form is valid or not, where should i manipulate the data? 因此,在将表单数据与实体绑定在一起并检查表单是否有效之间,我应该在哪里处理数据? do i need to bind the new end_date to the form as well? 我还需要将新的end_date绑定到表单吗? or just bind it to the entity? 或只是将其绑定到实体?

Thanks 谢谢

   if($form->isValid()) {
            $em = $controller->getDoctrine()->getManager();

            $days = $request->get("form")["days"] 
            //* calc end date here $endDate as \DateTime */
            $listing->setEndDate($endDate);

            $em->persist($listing);
            $em->persist($product);
            $em->flush();
            return true;
 }

Although @Evgeniy's answer is technically correct I want to expand it a little bit. 尽管@Evgeniy的答案在技术上是正确的,但我还是要扩大一点。

When you create a form in Symfony and then call the handleRequest method, the form will be populated with all of the submitted fields. 当您在Symfony中创建表单,然后调用handleRequest方法时,该表单将填充所有提交的字段。 If you want to manipulate your form's data you have two possible options: 如果要操纵表单的数据,则有两个可能的选择:

  1. Do any necessary modifications in the Controller in the if($form->isValid()){} code block. Controller中的if($form->isValid()){}代码块中进行任何必要的修改。
  2. Create form events and manipulate the data before/after it has been populated. 创建表单事件并在填充数据之前/之后操作数据。 ( Form events , Dynamic form modifications ) 表单事件动态表单修改

Both options are equally right. 两种选择都是正确的。 What you choose depends on your use case: 选择什么取决于您的用例:

  1. If you're going to be using this FormType in one particular place, you can easily go with defining your logic inside the Controller . 如果要在某个特定位置使用此FormType ,则可以轻松地在Controller定义逻辑。
  2. If, however, your FormType is going to be included in other places and needs to maintain the same functionality you should go with writing form event listeners/subscribers. 但是,如果您的FormType将包含在其他位置,并且需要维护相同的功能,则应该编写表单事件侦听器/订阅者。 This will eliminate the need of writing any code inside the Controller and give you the final Entity ready to be saved into the database without the need of any further modifications (as they have already been done in the event listener you've created). 这将消除在Controller内部编写任何代码的需要,并使最终的Entity准备好保存到数据库中,而无需进行任何进一步的修改(因为它们已在创建的事件侦听器中完成)。

I recommend to you to do that in callback on prePersist. 我建议您在prePersist的回调中执行此操作。 This is an good practice. 这是一个好习惯。 See there more about this http://symfony.com/doc/current/book/doctrine.html#lifecycle-callbacks 有关此信息,请参见http://symfony.com/doc/current/book/doctrine.html#lifecycle-callbacks

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM