cakePHP afterFind 和关联值

发布于 2025-01-06 00:08:38 字数 753 浏览 0 评论 0原文

我使用 cakePHP 2.0 afterFind() 回调在显示数据库值之前对其执行计算。

我有三个模型 - 材料、产品和报价。

每个产品都有一种材料,并且使用此关联在 Product afterFind 回调中执行计算,特别是使用以下行:

$results[$key]['Product']['material_cost'] = $results[$key]['Product']['material_mass'] * $val['Material']['cost'];

with $val['Material']['cost'] 引用关联的材料。

到目前为止一切都很好。

接下来,我的 Quote 模型中有一个 afterFind() 回调。报价与产品相关联,报价模型中的计算取决于正在发生的产品模型中的计算 - 特别是引用材料的计算。

我可以在 Quote 模型 afterFind 中引用 Product 模型 afterfind 结果,如下所示: $val['Product']['number_tools']

但是,现在 Product 模型无法找到关联的材料,我得到错误: 未定义索引:材质 [APP/Model/Product.php,第 126 行]。
Product.php 第 126 行是

$val['Material']['cost'] 

如何解决这个问题,这让我发疯!

I'm using cakePHP 2.0 afterFind() callback to perform calculations on database values before it is displayed.

I have three models - Materials, Products and Quotes.

Each product has a material and calculations are performed in the Product afterFind callback using this association, specifically with the line:

$results[$key]['Product']['material_cost'] = $results[$key]['Product']['material_mass'] * $val['Material']['cost'];

with $val['Material']['cost'] referring to the associated material.

All fine so far.

Next I have an afterFind() callback in my Quote model. The quote is associated with a product and the calculations in the quote model are dependant on the calculations in the Product model taking place - specifically the one which referances the material.

I can reference the Product model afterfind results in the Quote model afterFind just fine like: $val['Product']['number_tools']

However, now the Product model cannot find the associated material and I get the error:
Undefined index: Material [APP/Model/Product.php, line 126].
on line 126 of Product.php is

$val['Material']['cost'] 

How can I get around this problem, it's driving me nuts!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

芸娘子的小脾气 2025-01-13 00:08:38

您需要加载材质模型。您可以从产品访问它的原因是因为它是通过关系关联的,并且您的递归默认允许模型加载相关数据。因此,在 Quote 模型 beforeFind 方法中,在开始解析数据之前,加载材质模型:

public function afterFind($results) {

    App::uses('Material', 'Model');
    $material = new Material();
    $materials = $material->find(...);

    foreach ($results as $key => $val) {
        // access $materials['Material']['cost']
    }
    return $results;
}

You need to load the model for Material. The reason you can access it from the Product is because it is associated by relationship and your recursive is by default allowing the model to load the related data. So in the Quote model beforeFind method, before you start parsing through the data, load the Material Model:

public function afterFind($results) {

    App::uses('Material', 'Model');
    $material = new Material();
    $materials = $material->find(...);

    foreach ($results as $key => $val) {
        // access $materials['Material']['cost']
    }
    return $results;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文