cakePHP afterFind 和关联值
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要加载材质模型。您可以从产品访问它的原因是因为它是通过关系关联的,并且您的递归默认允许模型加载相关数据。因此,在 Quote 模型 beforeFind 方法中,在开始解析数据之前,加载材质模型:
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: