gpt4 book ai didi

php - 防止通过 afterFind 更改的数据以这种方式更新?

转载 作者:搜寻专家 更新时间:2023-10-31 20:53:57 28 4
gpt4 key购买 nike

如果我使用 modelafterFind 函数根据它们的 GEO 位置调整数据,如下所示:

 #app/models/product.php
function afterFind($results)
{
if( $userInArea)
{
if(!isset($results[0]['Product']['price']))
{
return $results;
}
foreach ($results as $key => $val)
{
$results[$key]['Product']['price'] = $this->priceAdjustAfterFind($val['Product']['price']);
}

return $results;
}
}

function priceAdjustAfterFind($price)
{
return $price * 1.2;
}

如果在某个区域,这将使模型返回的数据的价格字段高出 20%。

问题是:

假设价格是 100。管理员在 GEO 区域编辑产品 admin/product/edit/4product model 提供给 edit view 的数据会将价格提高到 120,因为她在 GEO 区域,并且会触发价格调整。

因此,一旦她保存(比如她调整了名称),她就会无意中将价格从 100 更改为 120。现在,如果她再次编辑,它将加载为 144,每次都会增加。

我没有想出一个合乎逻辑的方法来防止这种情况发生。有什么建议吗?

最佳答案

您的代码需要重构,价格字段的有条件更新不是一个好主意。

$results[$key]['Product']['price_adjusted'] 
= $this->priceAdjustAfterFind($val['Product']['price']);

制作一个 View 助手

<?php
class PricingHelper extends AppHelper {
function show($result) {
return ($result['price_adjusted']) ? $result['price_adjusted'] : $result['price']
}
}

在你的 View 中调用

<?php echo $this->Pricing->show($result);  ?>

关于php - 防止通过 afterFind 更改的数据以这种方式更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4629687/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com