Answer the question
In order to leave comments, you need to log in
How to implement deletion of files in Yii when deleting related records from the database?
Good afternoon.
When deleting a record from the database, it is necessary to delete the corresponding files from the disk.
To do this, afterDelete() makes an entry in a special table with a list of files to be deleted. And then the files from this table are deleted by krone.
At the same time, all related records are automatically deleted at the database level. They also need to delete files. Well, it is clear that when you delete them through foreign keys, the files remain.
I see two ways to solve the problem:
1. In the beforeDelete() of the parent model, delete all related ones. Those. do it at the application level. But there are a lot of related records and this will create a rather heavy overhead.
2. Hang a trigger on the table and make a note about deleting the file through it. But in this case, a problem arises: the path to the file is calculated based on several fields. And this is done dynamically. And in this case, this logic will be duplicated in the database and in the application. Then you can get confused.
Tell me how to get out of this situation?
Answer the question
In order to leave comments, you need to log in
1. replace the cron with a task queue (rabbitmq/zeromq)
2. It is better to store the deletion logic in the model, and even better to make a service that will resolve all this (so that there would be no extra crap during deployment). You can also implement delayed deletion (again via the queue manager) where all the heavy stuff will happen. In any case, it is better to take it to a separate service.
In the trigger, save a set of fields on the basis of which the path is calculated, and then run a console yii application using cron, where the path calculation function is already known.
In order of general nonsense:
3. Store files in the database (bad option, but problem-free)
4. Delete files directly from the database (hey, you have postgres! You can write stored logic to it in python and perl)
5. Simplify the calculation of the file path to the built-in function (roughly speaking, md5(filename)),
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question