Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将从表单收集的数据在持久化到实体之前进行转换_Php_Forms_Symfony_Orm - Fatal编程技术网

Php 将从表单收集的数据在持久化到实体之前进行转换

Php 将从表单收集的数据在持久化到实体之前进行转换,php,forms,symfony,orm,Php,Forms,Symfony,Orm,我是Symfony2的新手,只是通过一些现有的代码库进行黑客攻击。我需要帮助了解如何实现以下结果 我有一个PriceList实体存储: DollarCostPrice, PercentDiscount1, PercentDiscount2, PercentCommission 我希望用户通过表单输入以下4个值: SalePrice, DollarDiscount1Price, DollarDiscount2Price, PercentCommission 在哪里, SalePrice = (

我是Symfony2的新手,只是通过一些现有的代码库进行黑客攻击。我需要帮助了解如何实现以下结果

我有一个PriceList实体存储:

DollarCostPrice, PercentDiscount1, PercentDiscount2, PercentCommission
我希望用户通过表单输入以下4个值:

SalePrice, DollarDiscount1Price, DollarDiscount2Price, PercentCommission
在哪里,

SalePrice = (DollarCostPrice + (PercentCommission * DollarCostPrice))

DollarDiscount1Price = ((DollarCostPrice + (PercentCommission * DollarCostPrice)) * (100 - PercentDiscount1)/100)

DollarDiscount2Price = ((DollarCostPrice + (PercentCommission * DollarCostPrice)) * (1 - PercentDiscount2)/100)
但一旦用户输入了上述值,我将计算需要保存在实体中的
DollarCostPrice、PercentDiscount1、PercentDiscount2、PercentCommission

我希望在表单中使用上述4个字段的集合,以便用户可以一次为多个项目输入此信息


在Symfony2中使用表单、集合和数据转换器,我还是新手。如果有人能帮我确定最好的方法,我将不胜感激。

我会这样做。注意,我在这里做假设,例如,您没有提供与PriceList相关的实体的任何信息

  • 创建一个PriceListType表单类型,您将有1个映射字段(百分比佣金)和3个非映射字段(SalePrice、DollarDiscount1Price、DollarDiscount2Price)
  • 要添加未映射的字段,请执行以下操作:

    ->添加('SalePrice',null,数组('mapped'=>false))

    我假设PriceList与某种SKU对象或类似对象有关。你需要考虑如何管理这种关系。

  • 从价格表类型的基本表单开始。我会使用类似的方法来添加新的价目表项目
  • 在保存时,我最初只是在控制器中进行实体计算,以快速启动和运行

        $yourForm->handleRequest($request);
        if ($yourForm->isValid()) {
    
            // loop through elements in your collection - note for non mapped fields
            // you need to access them like so: $form->get("DollarDiscount1Price")->getData();
    
            // calculate the data you wish to save to each PriceList entity, set and persist it
    
            $entityManager->persist($priceListEntity);
    
            // finish & finally flush
            $entityManager->flush();
        }
    
    然后,稍后我可能会将该功能移动到表单事件侦听器

    我自己认为这里不需要数据转换器,因为它会增加复杂性。如果你打算在许多形式上重复使用这个功能,也许,但我不认为我会担心它


    人们会告诉你X是理想的解决方案,但我会从简单开始,随你去完善。

    从这开始,我会回答你的问题。在这里发布之前,我实际上浏览了这些文档。那里的示例过于简单,我无法理解如何将集合的概念组合在一起,并将数据转换应用于多个值。