data backup table

Friends!! Good Morning!! please I need help with this little problem.

I made a patient management system for medical centers, the system works very well, the problem is that I realized that when removing a patient or another field, the data erased them directly from the root, and they should not because even if the staff disappears there should be a record in the database.

How could I solve this small inconvenience?

I thought about creating a table that contained the same data to use as a backup, but I really do not know how this logic could be applied, since I can not find a way to save them in the backup table when I save a new record in the table that already exists, please help.

It is possible that the associated data is “automagically” being deleted because of cascadeCallbacks (See this page).

I do not know how the rules regarding such data are in the country you reside, but I’d definitely recommed you to not even delete such data at all, but “soft-delete” it.
Effectivley, this means that you add a field “deleted” to your database-tables that you set to true, if someone pushes the delete-button.

This way, nothing is deleted accidentally; historiy-data can be accessed all the time (though you can still label it as an archive in the view) and you don’t need a second (or even more) table to save the data you already have saved before (which could cause errors when copying the data as well).

This, of course, does not mean that all the data that is stored in your database should’nt be backed up, but this is typically something that you hosting provider can (and imho should) take care of.

Best regards,

1 Like

You should use a ‘soft-delte’ column, if the column is null the record y active, if the column is not null it is considered deleted but kept for historical reasons

You can use plugins for that like Muffin/Trash

Thank you very much for answering!. In fact, I thought about doing that by placing one more field in the doctor’s box, either active or inactive, in addition to hiding the sight data and filtering it by the condition that it only shows them in case the status is active. Until then it works for me, but I run into a big problem, when I add a medical appointment or another procedure in which I need the doctor’s identification, it shows me the list of doctors (active and inactive). and what I need to show in that part are only the doctors with the active state, that is, I can hide the inactive doctors from the view, but when I need to add a query and give me the doctor’s identification, it shows them all :frowning:

You can set filter on relations too :wink:
Basically it works like so:

$this->MedicalAppointments->find()->contain([‘Doctors’])->where([‘Doctors.deleted’ => false])->all();

Thanks for your answer! I will document about this :wink:

Thank you! Your answer helped me a lot. :grin:

In that filter could I put it that only brings me the data taking into account the search by means of a state? is to say something like this:

$this->MedicalAppointments->find()->contain([‘Doctors’])->where([‘Doctors.estatus’ => ‘active’]);

How could he do it? :thinking:

You can filter any value you like on any field that directly belongs to MedicalAppointments (as of the example) or any associated entity - though that entity needs to be contain()'ed

I’ve found this part of the documentation very helpful =)

:open_mouth: Perfect! thank you very much. I’m going to document myself about the aforementioned :sunglasses: :smiley: