Building the Model
The model handles all database interactions. It provides five methods that cover the full lifecycle of a file record: listing, counting, retrieving, inserting, and deleting.
The Full Model
PHP
<?php
class File_manager_model extends Model {
private string $table_name = 'file_manager';
public function get_all_files(
int $limit,
int $offset,
string $search_query = '',
string $search_column = ''
): array {
$where_clause = '';
if ($search_query !== '') {
$col = ($search_column !== '')
? $search_column
: 'doc_name';
$where_clause = 'WHERE ' . $col
. ' LIKE :search_query';
}
$sql = 'SELECT id, doc_name, file_name,
file_size, file_type
FROM ' . $this->table_name . '
' . $where_clause . '
ORDER BY id DESC
LIMIT :limit OFFSET :offset';
$params = [
':limit' => (int) $limit,
':offset' => (int) $offset
];
if ($search_query !== '') {
$params[':search_query']
= '%' . $search_query . '%';
}
return $this->db->query(
$sql, 'object', $params
);
}
public function count_all_files(
string $search_query = '',
string $search_column = ''
): int {
$where_clause = '';
if ($search_query !== '') {
$col = ($search_column !== '')
? $search_column
: 'doc_name';
$where_clause = 'WHERE ' . $col
. ' LIKE :search_query';
}
$sql = 'SELECT COUNT(*) as count
FROM ' . $this->table_name . '
' . $where_clause;
$params = [];
if ($search_query !== '') {
$params[':search_query']
= '%' . $search_query . '%';
}
$result = $this->db->query(
$sql, 'object', $params
);
return (int) ($result[0]->count ?? 0);
}
public function get_file_by_id(
int $update_id
): array|false {
$record_obj = $this->db->get_where(
$update_id, $this->table_name
);
if ($record_obj === false) {
return false;
}
return [
'id' => (int) $record_obj->id,
'doc_name' => $record_obj->doc_name,
'file_name' => $record_obj->file_name,
'file_path' => $record_obj->file_path,
'file_size' => (int) $record_obj->file_size,
'file_type' => $record_obj->file_type
];
}
public function insert_file(array $data): int {
return $this->db->insert(
$data, $this->table_name
);
}
public function delete_file(int $update_id): void {
$this->db->delete(
$update_id, $this->table_name
);
}
}Method Summary
| Method | Returns | Used By |
|---|---|---|
get_all_files() |
Array of file objects | manage() - populates the paginated table |
count_all_files() |
Integer | manage() - provides total for pagination |
get_file_by_id() |
Associative array or false | show(), download(), confirm_delete() |
insert_file() |
New record ID | submit() - stores uploaded file metadata |
delete_file() |
Void | submit_confirm_delete() - removes database record |
Unlike the Countries model from the Basic CRUD chapter, this model does not include an update() method. File records are insert-once - you cannot change a file after upload (you would delete the old record and upload a replacement). The doc_name could theoretically be edited, but we omit that for simplicity.
We're continually improving the Trongate documentation. If anything is incorrect, unclear, incomplete, or could be better, we'd genuinely appreciate your input.
Share your thoughts in the Documentation Feedback.