保存到与 CakePHP 1.3 中该模型的另一个实例相关的模型

发布于 2024-10-14 10:34:34 字数 510 浏览 4 评论 0原文

我对蛋糕还比较陌生,我不得不再次研究它如何做才能得到我想要的东西。我真的很想找到做我需要的事情的“正确”方法。

我正在构建一个“绩效”系统,每个月都会使用该系统来回顾某人上个月完成目标的情况。

这个月的回顾设定了新的目标,也记录了上个月的结果。

我有一个评论模型,它有很多目标。

在本月评论的编辑视图中,我显示了上个月评论和评论的一些数据。目标中的一些字段与上个月的回顾相关。

我需要保存上个月每个目标的结果,并且也能够设置本月的目标 - 因此我将数据保存到与当前审核相关的目标以及另一个目标。

当我尝试保存“不相关”目标时,CakePHP 强制 review_id 成为当前目标的 id - 我什至尝试在 Review::afterSave 回调中循环并检查每个 review_id - 尽管我可以将正确的 review_id 值发送到目标->保存,数据库仍然会使用当前的评论 ID 进行更新,而不是我设置的。

谁能告诉我保存到与当前模型的另一个实例相关的模型的“蛋糕”方法?这可能吗?

I'm relatively new to cake, and once again I'm having to work around how it does things to get what I want. I'd really love to find out the "correct" way of doing what I need.

I'm building a "performance" system, that's used each month to review how someone did against their goals from last month.

This month's review sets new goals, and also records the result against last month.

I have a Review model, which hasMany Goals.

In the edit view for this months Review I display some data from last month's Review & some fields from the Goals related to last month's Review.

I need to save the result for each of last months Goals, as well as to be able to set this months Goals too - so I'm saving data to Goals related to the current Review, as well as another one.

When I try to save the "unrelated" Goals, CakePHP forces the review_id to be the id of the current one - I even tried looping in the Review::afterSave callback and checking each review_id - although I can send the right review_id value to Goal->save, the database still gets updated with the current Review id, rather than the one I set.

Can anyone tell me the "cake" way to save to models related to another instance of the current model? Is this even possible?

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

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

发布评论

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

评论(1

三五鸿雁 2024-10-21 10:34:34

好吧,这可能不是最有效的方法 - 但我已经到了最后期限。

因此,我没有检索上个月的评论,而是创建了另一个“hasMany”关系,使用具有子选择的 finderQuery 来查找上个月查询的 ID,并使用它来选择具有该 review_id 的目标。它使用 {$__cakeID__$} 查找当前评论的 ID,如下所述:

http://book.cakephp.org/view/1039/Associations-Linking-Models-Together#hasMany-1043

因为一切都在一个按照 Cake 的理解,事情会在 saveAll 上自动更新。

这意味着我有一个荒谬的子选择查询,将上个月的目标与本月的回顾相关联,但它有效。

有一天我会弄清楚如何优化它,这样它就不会让我的数据库哭泣......

Ok, this may not be the most efficient way of doing it - but I'm on a deadline.

So, instead of retrieving last month's Review, I created another "hasMany" relationship, using a finderQuery that has a subselect to find the ID of the last months query, and use that to select the Goals with that review_id. It uses {$__cakeID__$} to find the ID of the current Review as explained here:

http://book.cakephp.org/view/1039/Associations-Linking-Models-Together#hasMany-1043

Because everything's related to each other in a way that Cake understands, things just update automatically on saveAll.

It means I have a ridiculous subselect query relating the last months Goals to this months Review, but it works.

One day I'll figure out how to optimise it so it doesn't make my database cry....

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文