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,
Daniel
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 
You can set filter on relations too 
Basically it works like so:
$this->MedicalAppointments->find()->contain([âDoctorsâ])->where([âDoctors.deletedâ => false])->all();
Thanks for your answer! I will document about this 
Thank you! Your answer helped me a lot. 
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? 
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 =)