Php 将从表单收集的数据在持久化到实体之前进行转换
我是Symfony2的新手,只是通过一些现有的代码库进行黑客攻击。我需要帮助了解如何实现以下结果 我有一个PriceList实体存储:Php 将从表单收集的数据在持久化到实体之前进行转换,php,forms,symfony,orm,Php,Forms,Symfony,Orm,我是Symfony2的新手,只是通过一些现有的代码库进行黑客攻击。我需要帮助了解如何实现以下结果 我有一个PriceList实体存储: DollarCostPrice, PercentDiscount1, PercentDiscount2, PercentCommission 我希望用户通过表单输入以下4个值: SalePrice, DollarDiscount1Price, DollarDiscount2Price, PercentCommission 在哪里, SalePrice = (
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相关的实体的任何信息
$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是理想的解决方案,但我会从简单开始,随你去完善。从这开始,我会回答你的问题。在这里发布之前,我实际上浏览了这些文档。那里的示例过于简单,我无法理解如何将集合的概念组合在一起,并将数据转换应用于多个值。