Saving to a model related to another instance of this model in CakePHP 1.3
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 s开发者_开发知识库omeone 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?
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....
精彩评论