gpt4 book ai didi

cakephp 行为 afterFind 没有在相关模型上调用

转载 作者:行者123 更新时间:2023-12-04 15:04:27 25 4
gpt4 key购买 nike

我正在使用 afterFind 函数来修改 find 函数中的数据。它工作正常。如果我将 afterFind 函数移动到一个行为中(在插件中),它仍然可以工作,但只有当感兴趣的模型是主要模型时,即当模型属于另一个模型时它不会被调用。有没有办法解决这个问题?我正在使用蛋糕 1.3.4。这是行为的简化版本:

class ChemicalStructureBehavior extends ModelBehavior {
function afterFind(&$model, $results, $primary) {
foreach ($results as &$unit) {
// format chemical formula (with subscripts)
$unit[$model->alias]['chemical_formula_formatted'] = preg_replace('/([0-9]+)/i', '<sub>$1</sub>', $unit[$model->alias]['chemical_formula']);
}

return $results;
}
}

最佳答案

我想我会根据代码块的一般应用情况做两件事之一:

  • 通用版本:不使用行为,但在 AppModel::afterFind 中包含您的方法 block
  • 手术版:使用一种行为并将其附加到需要共享功能的每个模型上。
  • 关于cakephp 行为 afterFind 没有在相关模型上调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4028441/

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