Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Document Versioning
  • Hey guys,
    Does anyone have a suggestion for document versioning/revisions using either the fuel orm or database classes? I once worked with a kohana orm module that allowed users to restore a revision of a document. That module also created "copies" or revisions of a record on all DB updates. Is there an easier/better way of doing something like that?
  • Sounds like a job for an observer. Observers are like plugins you can use with the ORM to execute specific tasks at events within the ORM, like just before or after a query. Some are included, have a look at how they work. I think there's also some basic documentation available.
  • I saw those and they look really interesting. Does this workflow make sense: Table Schema:
    I'm trying to keep MySQL joins to minimum by using the REVISIONS_ID to group the revisions. The REVISIONS_ID would be a random key ID shared between all revisions of the same record. TABLE: ID, REVISION, REVISIONS_ID, ... Flow:
    1. Inserting a new record. The Observer "before" would check if it's a new record and if it is, die.
    2. Updating a record. The Observer "before" would check and find out it's an update. It would then update (increment) the revision and insert the update as a new record with the same REVISIONS_ID.
  • Moved this topic to the ORM forum. I'm don't have the in-depth knowledge about the observer mechanism to say if it's possible to alter the ORM flow from within an observer (as in skipping the operation, or changing the update to an insert). Maybe Jelmer can comment?
  • I would do this... in before_insert I would create a new revision_id (however you wanted to store it)... and not pass data into it.
    then in before_save I would have it create a new revision... So the logic here is basically... initialize/setup revision on first insert... then on each update add a revision. If you check out how the CreatedAt and Updated observers are coded, it will be very very simple to create something from that code... the logic is there. Just combine the two and add your appropriate fields in place of created_at and updated_at.
  • Thanks @nerdsrescueme... But after thinking about the clutter of revisions mixed in with "latest" versions I thought of the follwoing setup: (I want to implement your idea of using the before_insert, and before_save)
    Table  Name: Documents
    id|  revision| other_columns
    1|  1|   stuff 1     <- created by inserting new record
    2|  1|   stuff 1     
    3|  4|   stuff 4     <- updated 4 times
    
    Table Name: Documents_Revisions
    id|  documents_id| revision| other_columns
    1|  3|    1|   stuff 1
    2|  3|    2|   stuff 2
    3|  3|    3|   stuff 3
    4|  3|    4|   stuff 4
    
  • Then it becomes a question of semantics... You could still use the same setup on the documents model and just write to the revisions table, but I personally wouldn't mix another models data into a model... Sounds like a job for the controller. :)

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion