Saturday, January 30, 2010

how to save data from a model and all related models to a new table on 'completion'?

I am building a 'work_order' table that I need to convert to an
invoice once it's been flagged as 'completed'. I need to save all the
related models' information also when converting so that I can go back
and look at an invoice and see all the related data too, even if the
original related data to the 'work_order' table has been changed or
deleted.

I'll use the standard blog schema to make it easy. I have a Post model
that hasMany Comments, HABTM Tags, and belongsTo User. In the Post
table there is a boolean column for 'published'. When the Post is
flagged as 'published' I'd like to copy over the data from the Post
into a new table for archiving purposes but I also need to copy the
Users, Comments and Tags data that are related to that Post to their
respective 'archived' tables so that if a User gets changed (i.e.
address or phone changes) or deleted from the User table the row of
user information is still in the archived table as it was originally.

Is there an easy or good way to convert the data to a new table for
archives? Going back to the real project tables of using a work_order
and invoice: Once a work_order has been flagged as 'completed' on the
afterSave maybe create an array with all the related data 'belonging'
to Invoice and save it all? If you did it that way how would you save
the Invoice from the WorkOrder model since they're not really related?

Does anyone have a good practice or suggestions on how it should be
done?

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: