ria/app/Models/Ipattachment_model.php
2024-09-11 08:54:25 +05:30

78 lines
1.9 KiB
PHP
Executable File

<?php
namespace App\Models;
use CodeIgniter\Model;
class Ipattachment_model extends Model
{
/**
* This function is used to add new user to system
* @return number $insert_id : This is last inserted id
*/
function addNewAttachment($attachments)
{
$this->db->transStart();
$builder = $this->db->table('ip_invoice_attachment');
$builder->insert($attachments);
$insert_id = $this->db->affectedRows();
$this->db->transComplete();
return $insert_id;
}
// Ipinvoice_model
public function deleteAttachment($invoice_attachment_id) {
// Get the attachment details to get the file path
$builder = $this->db->table('ip_invoice_attachment');
$builder->where('invoice_attachment_id', $invoice_attachment_id);
$query = $builder->get();
$attachment = $query->getRowArray(); // Get single row as an associative array
if ($attachment) {
// Construct the file path
$fileName = $attachment['attachment_name']; // Make sure this is the correct field name
$filePath = './public/uploads/images/invoice_files/' . $fileName;
// Debug: Log the file path
log_message('error', 'Attempting to delete file: ' . $filePath);
// Check if the path is actually a file
if (file_exists($filePath) && !is_dir($filePath)) {
unlink($filePath); // Delete the file
} else {
log_message('error', 'File does not exist or is a directory: ' . $filePath);
}
// Delete the record from the database
$builder->where('invoice_attachment_id', $invoice_attachment_id);
$builder->delete();
return $this->db->affectedRows() > 0;
}
return false;
}
}