Change model from DB to File

Hi:

I have a model which retrieves/process data from a MySQL table.
Due to some limitations we have found (storing very big data in JSON format in the table, and trying to recover them, we hit the max_allowed_packet problem limitation (1GB maximum output)), so we are thinking to switch this approach to a file storage based model.

Basically I want to retrieve the information from the DB, if record already exists, otherwise, retrieve the info from the corresponding saved file. Also, on create a new record, always create the record in file. (don’t use the db table anymore, only for old records)

What kind of approach do you suggest me to use? Create a new data provider like the one here ?? Overwrite/extend the ActiveRecords methods (for example, save() , etc) in the current model to achieve what I want ??

Thank you so much!

[Update]: Other approach will be, insert new records in the DB with the minimum data but with an special flag (field in the table) to let me know that we also have to create a JSON file in the storage with the full content. While recovering a record from this table, if the record has that special flag set to true (read_from_file => true), populate the record with the info from an specific file (JSON file) ?? Does anyone how to achieve this ?

Hi There,

you can use a column for json file name and make use of behavior functions in the ActiveRecord model

like

beforeSave();

afterSave();

beforeUpdate();

afterUpdate();

Do the write and update of JSON file through these events.

1 Like

Thank you @arojohnson,

That’s what I’ve done. Implemented the beforeSave, just avoiding saving the big data fields to the DB, and saving that info to files, and on afterFind implemented the overloading of the model parameters readed from the JSON file, in case the file exists. This way it works for old records and for new ones. Also implemented the afterDelete method to delete the fields before deleting the associated record to it.

I’m a newbie with Yii (I worked with CI), and didn’t know about those special methods (after/before|Command) in the data methods.

So far so good. Thank you!!