Delete Child records with images


is there any way i can delete physical file of child record deleted when parent record delete?

i.e. i have two table, blog and comments

i am allowing user to upload images with comments.

now what i want is, when i delete any blog, all its comments and its associated image should deleted.

thanks for your help in advanced.

You should have provided more details. Are the images part of comment? If yes, are you storing the content of the comment as a blob in database table? Assuming those two to be true, you must have two models -

Post -




Comment -



you will have a relation in Post model -

‘comments’=>array(self::HAS_MANY, ‘Comment’, ‘comment_id’);

Now before you delete the post, you should delete all comment in the post ($postModel is the instance which has the post to be deleted)-

foreach($postModel->comments as $comment)