Historizing ActiveRecords

You can use this class to save ActiveRecords historized, which means

we dont do any updates or deletes.

On delete the deletedColumn is set to mark the record as deleted.

On update a new record is created an the old record is marked as deleted.

Create a table like the following:

CREATE TABLE customer (

id INT NOT NULL AUTO_INCREMENT, – name of this column is optional

itemId INT, – name of this column is itemId by default but can be set to whatever you like

– your columns here

created TIMESTAMP DEFAULT CURRENT_TIMESTAMP, – column is optional

deleted TIMESTAMP NULL DEFAULT NULL, – name of this column is itemId by default but can be set to whatever you like

PRIMARY KEY (id),

KEY (itemId),

);

Hope you like it.

I made some improvements:

  • In default scope, records are ordered ascending by their itemId and not their primaryKey.

  • After saving a new record, itemId is set in the model automatically (analog to the primaryKey which is also set in the model after saving the record)