- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
如果我使用 model
的 afterFind
函数根据它们的 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/4
。 product 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/
因此,当我在相应的模型/ Controller 中时,afterFind 工作得很好。但是,在调用关联模型时,发送到 afterFind 回调的数据的格式不同。这会导致 afterFind 崩溃,因为
laravel 中是否有任何 afterFind() 模型事件,如 cakePHP 中那样,以便我可以在任何搜索查询后格式化数据。或任何其他方式来做到这一点? 谢谢 最佳答案 这不是事件,而是您可以在
我正在使用 afterFind 函数来修改 find 函数中的数据。它工作正常。如果我将 afterFind 函数移动到一个行为中(在插件中),它仍然可以工作,但只有当感兴趣的模型是主要模型时,即当模
我正在尝试显示我们客户的各个时区以及由此得出的本地时间的列表。现在,在我的客户端模型中,我有以下函数 (doAfterFind),它只是 afterFind 的包装,提供一致的格式。 public f
如果我使用 model 的 afterFind 函数根据它们的 GEO 位置调整数据,如下所示: #app/models/product.php function afterFind($result
我有一个 cakePHP 应用程序,它从两个不同的数据库中提取数据,这两个数据库将日期和时间存储在来自不同时区的数据中。一个数据库的时区是 Europe/Berlin,另一个是 Australia/S
我正在尝试在模型上实现一个 afterFind Hook ,但不太清楚语义是什么。我将使用文档和其他 StackOverflow 问题作为指南,从反复试验中总结出以下内容。 我的目标是处理结果(通过应
在sails.js 中,模型支持验证、创建、更新和销毁的生命周期回调。 是否也支持 find() 或 query 的回调?像 beforeFind() 和 afterFind()? 这个想法是一样的。
我有两个模型 User 和 Email。 Email 有一个来自 User 的外键。 电子邮件在数据库中的值在保存到数据库之前被加密。并且在检索电子邮件时对其进行解密。因此,电子邮件在数据库中永远不会
CakePHP 的 Model::afterFind()回调看起来像: afterFind(array $results, boolean $primary = false) 根据文档: The $p
我有两个模型。 Purchase 和 PurchaseDetail 与它们相关联 Purchase.hasMany(models.PurchaseDetail) PurchaseDetail.belo
我试图在模型上运行 afterFind Hook ,但运行查询后,console.log 中的 JSON.stringify 结果似乎没有改变。但是,如果我专门记录该属性,数据就会更改。 // on
我在nodejs中有一个使用sequelize的项目。 我正在尝试在模型中执行 FindAll,但出现此错误: Cannot read property 'findAll' of undefined
我是一名优秀的程序员,十分优秀!