• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using beforeDelete in Model

#1
Hi,
Let's say I have a table called 'projects'. One of the columns contains a filename string. What i'd like to do, is to delete the file after/before deleting the row from the database. I see, that Codeigniter 4's models can now use a callback function. But i don't know how can I access data, particularly the filename field.



Code:
<?php namespace App\Models;

use CodeIgniter\Model;

class ProjectModel extends Model {

    protected $table      = 'projects';
    protected $primaryKey = 'id';

    protected $returnType     = 'App\Entities\Project';
    protected $useSoftDeletes = false;

    protected $allowedFields = ['id', 'title', 'desc', 'filename'];

    protected $useTimestamps = true;
    protected $createdField  = 'created_at';
    protected $updatedField  = 'updated_at';

    protected $afterDelete = ['deleteFiles'];

    protected function deleteFiles() {
        // here i would like to delete the file
    }


}

Or am I approaching this the wrong way? Should I just get the data in my Controller, delete the file and then delete the row from database? I would do that in CI3 this way, but now i discovered the callbacks in CI4 and I consider this a more elegant way/better practice. Thanks.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.