FriendsOfCake/cakephp-upload

afterDelete not working when deleting from belongsTo

emilushi opened this issue · 5 comments

So... I do have a model called Notes and another model called Attachments. The Attachments model belongsTo Notes and Notes hasMany Attachment.

With some modifications I was able to make it possible to add multiple Attachments at the same time from NotesController when creating a new Note.
And what is expected to happen when you delete a Note is: Delete note, Delete all attachments related to that Note and as well delete all files uploaded for those attachments.

The files are never deleted.

What I ended up doing is:
Removed webroot for file dir with beforeSave as below:

public function beforeSave(Event $event, Entity $entity, ArrayObject $options): void
{
    $dir = str_replace('webroot/', '', $entity->get('dir'));
    $entity->set('dir', $dir);
}

On this way the dir of the file will not contain weebroot in my case it will be: files/Attachments/attachment This as well helped me echoing the file url with: $this->Url->build($file->dir.$file->name)

And for deleting I needed to add an afterDelete as below:

public function afterDelete(Event $event, Entity $entity, ArrayObject $options): void
{
    $file = new File($entity->get('dir').$entity->get('name'));
    $file->delete();
}
ADmad commented

Have you enabled cascading callbacks for your associations? Without that the ORM will use deleteAll() to delete the associated records and hence afterDelete callbacks won't be triggered for them.

Have you enabled cascading callbacks for your associations? Without that the ORM will use deleteAll() to delete the associated records and hence afterDelete callbacks won't be triggered for them.

I have enabled of course.

As well I had some issues when adding a record for Notes table with multi attachments.

This didn't work for me:

if ($this->request->is('post')) {
    $note = $this-Notes->newEntity($this->getRequest()->getData(), [
        'associated' => ['Attachments']
    ])

    $this-Notes->save($note);
}

attachments doesn't show at all when I debug the Notes entity, and of course if I debug $this-getRequest()->getData() the attachments items are there.

Instead I had to:

$note = $this->Notes->newEntity();
if ($this->request->is('post')) {

    $files = $this->getRequest()->getData('attachments') ?? [];

    $note = $this-Notes->patchEntity($note, $this->getRequest()->getData())
    
    if($this-Notes->save($note)) {

        array_walk($files, function ($item, $key) use (&$files, $note) {

             if (is_array($item)) {
                 $files[$key]['foreign_key'] = $note->id;
             }

             return $files;
        });

        $attachmentsTable = TableRegistry::getTableLocator()->get('Attachments');
        $attachments      = $attachmentsTable->newEntities($files);
        $attachmentsTable->saveMany($attachments);

    }

}

Only on this way I was able to make the relationship work.

ADmad commented

attachments doesn't show at all when I debug the Notes entity

Have you set the attachments property accessible in the entity?

attachments doesn't show at all when I debug the Notes entity

Have you set the attachments property accessible in the entity?

I feel so dumb right now. I generated the code with bake and the attachments was added later 😞