From 5128e76896f140d71d4f7332abf6bc65787bd9af Mon Sep 17 00:00:00 2001 From: 0x1026 <69076992+0x1026@users.noreply.github.com> Date: Tue, 10 Dec 2024 22:57:34 +0100 Subject: [PATCH] feat: add updated_at and deleted_at fields to model mapping --- app/src/app/Models/BaseModel.php | 43 +++++++++++++++++---------- app/src/app/Models/Contract.php | 2 ++ app/src/app/Models/Element.php | 2 ++ app/src/app/Models/ElementType.php | 16 +++++----- app/src/app/Models/Incidence.php | 2 ++ app/src/app/Models/Machine.php | 2 ++ app/src/app/Models/Photo.php | 2 ++ app/src/app/Models/Point.php | 2 ++ app/src/app/Models/PruningType.php | 2 ++ app/src/app/Models/Role.php | 2 ++ app/src/app/Models/Route.php | 2 ++ app/src/app/Models/Sensor.php | 4 ++- app/src/app/Models/SensorHistory.php | 2 ++ app/src/app/Models/Task.php | 2 ++ app/src/app/Models/TaskType.php | 14 +++++---- app/src/app/Models/TreeType.php | 3 ++ app/src/app/Models/User.php | 2 ++ app/src/app/Models/WorkOrder.php | 14 +++++---- app/src/app/Models/WorkReport.php | 18 ++++++----- app/src/app/Models/Zone.php | 4 +++ app/src/app/Models/ZonePredefined.php | 18 ++++++----- 21 files changed, 107 insertions(+), 51 deletions(-) diff --git a/app/src/app/Models/BaseModel.php b/app/src/app/Models/BaseModel.php index cc58c913..8df0f65c 100644 --- a/app/src/app/Models/BaseModel.php +++ b/app/src/app/Models/BaseModel.php @@ -10,6 +10,10 @@ abstract class BaseModel protected ?string $created_at; + protected ?string $updated_at; + + protected ?string $deleted_at; + // Insert multiple records into the table public static function bulkInsert(array $records): void { @@ -21,8 +25,7 @@ public static function bulkInsert(array $records): void $query .= implode(", ", array_fill(0, count($records), "(" . implode(", ", $placeholders) . ")")); $params = []; - foreach ($records as $index => $record) - foreach ($record as $key => $value) + foreach ($records as $index => $record) foreach ($record as $key => $value) $params["{$key}_{$index}"] = $value; Database::prepareAndExecute($query, $params); @@ -40,7 +43,7 @@ public function belongsTo(string $relatedModel, string $foreignKey, string $owne $query = "SELECT * FROM {$relatedTable} WHERE {$ownerKey} = :foreignKeyValue LIMIT 1"; $results = Database::prepareAndExecute($query, ['foreignKeyValue' => $foreignKeyValue]); - return ! empty($results) ? $relatedModel::mapDataToModel($results[0]) : null; + return !empty($results) ? $relatedModel::mapDataToModel($results[0]) : null; } // Many-to-Many relationship @@ -75,7 +78,7 @@ public function belongsToMany( $results = Database::prepareAndExecute($query, ['localKeyValue' => $localKeyValue]); - if (! is_array($results)) + if (!is_array($results)) $results = []; // Process results @@ -85,7 +88,7 @@ public function belongsToMany( if ($withPivot) { // Attach pivot data as a property $relatedInstance->pivot = array_filter($row, function ($key) use ($relatedModel) { - return ! property_exists($relatedModel, $key); + return !property_exists($relatedModel, $key); }, ARRAY_FILTER_USE_KEY); } @@ -99,7 +102,7 @@ public static function count(array $conditions = []): ?int $query = "SELECT COUNT(*) as count FROM " . static::getTableName(); $params = []; - if (! empty($conditions)) { + if (!empty($conditions)) { $query .= " WHERE "; $query .= implode(' AND ', array_map(function ($key) { return "$key = :$key"; @@ -156,7 +159,7 @@ public static function find(string $id): ?object $results = Database::prepareAndExecute($query, ['id' => $id]); - return ! empty($results) ? static::mapDataToModel($results[0]) : null; + return !empty($results) ? static::mapDataToModel($results[0]) : null; } // Fetch all records from the table @@ -206,7 +209,7 @@ public static function findBy(array $conditions, bool $single = false): array|ob $results = Database::prepareAndExecute($query, $parameters); if ($single) - return ! empty($results) ? static::mapDataToModel($results[0]) : null; + return !empty($results) ? static::mapDataToModel($results[0]) : null; if (empty($results)) return []; @@ -217,7 +220,7 @@ public static function findBy(array $conditions, bool $single = false): array|ob // Fetch all soft deleted records public static function findSoftDeleted(): array { - if (! static::hasSoftDelete()) + if (!static::hasSoftDelete()) return []; $table = static::getTableName(); @@ -236,7 +239,7 @@ public function hasOne(string $relatedModel, string $foreignKey, string $localKe $query = "SELECT * FROM $relatedTable WHERE $foreignKey = :localKeyValue LIMIT 1"; $results = Database::prepareAndExecute($query, ['localKeyValue' => $localKeyValue]); - return ! empty($results) ? $relatedModel::mapDataToModel($results[0]) : null; + return !empty($results) ? $relatedModel::mapDataToModel($results[0]) : null; } // One-to-Many relationship @@ -249,7 +252,7 @@ public function hasMany(string $relatedModel, string $foreignKey, string $localK $results = Database::prepareAndExecute($query, ['localKeyValue' => $localKeyValue]); // Ensure $results is an array - if (! is_array($results)) + if (!is_array($results)) $results = []; return array_map(fn($row) => $relatedModel::mapDataToModel($row), $results); @@ -261,10 +264,10 @@ protected static function hasSoftDelete(): bool static $softDeleteCache = []; $table = static::getTableName(); - if (! isset($softDeleteCache[$table])) { + if (!isset($softDeleteCache[$table])) { $query = "SHOW COLUMNS FROM {$table} LIKE 'deleted_at'"; $result = Database::prepareAndExecute($query); - $softDeleteCache[$table] = ! empty($result); // Cache the result + $softDeleteCache[$table] = !empty($result); // Cache the result } return $softDeleteCache[$table]; @@ -277,7 +280,7 @@ public static function paginate(int $page = 1, int $perPage = 10, array $conditi $query = "SELECT * FROM " . static::getTableName(); $params = []; - if (! empty($conditions)) { + if (!empty($conditions)) { $query .= " WHERE " . implode(' AND ', array_map(fn($key) => "{$key} = :{$key}", array_keys($conditions))); $params = $conditions; } @@ -326,7 +329,7 @@ public function save(): void Database::prepareAndExecute($query, $properties); - if (! isset($this->id)) + if (!isset($this->id)) $this->id = Database::connect()->lastInsertId(); } @@ -344,4 +347,14 @@ public function getCreatedAt(): ?string { return $this->created_at; } + + public function getUpdatedAt(): ?string + { + return $this->updated_at; + } + + public function getDeletedAt(): ?string + { + return $this->deleted_at; + } } diff --git a/app/src/app/Models/Contract.php b/app/src/app/Models/Contract.php index e89eb9d1..600d6a79 100644 --- a/app/src/app/Models/Contract.php +++ b/app/src/app/Models/Contract.php @@ -32,6 +32,8 @@ protected static function mapDataToModel($data): Contract $contract->invoice_agreed = $data['invoice_agreed']; $contract->invoice_paid = $data['invoice_paid']; $contract->created_at = $data['created_at']; + $contract->updated_at = $data['updated_at']; + $contract->deleted_at = $data['deleted_at']; return $contract; } diff --git a/app/src/app/Models/Element.php b/app/src/app/Models/Element.php index 582c1dd3..35c20d5d 100644 --- a/app/src/app/Models/Element.php +++ b/app/src/app/Models/Element.php @@ -29,6 +29,8 @@ protected static function mapDataToModel($data): Element // $element->point_id = $data['point_id']; $element->tree_type_id = $data['tree_type_id']; $element->created_at = $data['created_at']; + $element->updated_at = $data['updated_at']; + $element->deleted_at = $data['deleted_at']; return $element; } diff --git a/app/src/app/Models/ElementType.php b/app/src/app/Models/ElementType.php index c5071c4f..4912cada 100644 --- a/app/src/app/Models/ElementType.php +++ b/app/src/app/Models/ElementType.php @@ -15,13 +15,15 @@ protected static function getTableName(): string protected static function mapDataToModel($data): ElementType { - $elementType = new self(); - $elementType->id = $data['id']; - $elementType->name = $data['name']; - $elementType->description = $data['description']; - $elementType->created_at = $data['created_at']; - - return $elementType; + $element_type = new self(); + $element_type->id = $data['id']; + $element_type->name = $data['name']; + $element_type->description = $data['description']; + $element_type->created_at = $data['created_at']; + $element_type->updated_at = $data['updated_at']; + $element_type->deleted_at = $data['deleted_at']; + + return $element_type; } //public function element(): Element diff --git a/app/src/app/Models/Incidence.php b/app/src/app/Models/Incidence.php index 6d360988..f33cc6be 100644 --- a/app/src/app/Models/Incidence.php +++ b/app/src/app/Models/Incidence.php @@ -26,6 +26,8 @@ public static function mapDataToModel($data): Incidence $incidence->description = $data['description']; $incidence->photo_id = $data['photo_id']; $incidence->created_at = $data['created_at']; + $incidence->updated_at = $data['updated_at']; + $incidence->deleted_at = $data['deleted_at']; return $incidence; } diff --git a/app/src/app/Models/Machine.php b/app/src/app/Models/Machine.php index 28a988e6..fa801de8 100644 --- a/app/src/app/Models/Machine.php +++ b/app/src/app/Models/Machine.php @@ -23,6 +23,8 @@ protected static function mapDataToModel($data): Machine $machine->max_basket_size = $data['max_basket_size']; $machine->photo_id = $data['photo_id']; $machine->created_at = $data['created_at']; + $machine->updated_at = $data['updated_at']; + $machine->deleted_at = $data['deleted_at']; return $machine; } diff --git a/app/src/app/Models/Photo.php b/app/src/app/Models/Photo.php index effccefb..bc5dfa2f 100644 --- a/app/src/app/Models/Photo.php +++ b/app/src/app/Models/Photo.php @@ -20,6 +20,8 @@ protected static function mapDataToModel($data): Photo $photo->name = $data['name']; $photo->path = $data['path']; $photo->created_at = $data['created_at']; + $photo->updated_at = $data['updated_at']; + $photo->deleted_at = $data['deleted_at']; return $photo; } diff --git a/app/src/app/Models/Point.php b/app/src/app/Models/Point.php index f434b472..843251bc 100644 --- a/app/src/app/Models/Point.php +++ b/app/src/app/Models/Point.php @@ -20,6 +20,8 @@ protected static function mapDataToModel($data): Point $point->latitude = $data['latitude']; $point->longitude = $data['longitude']; $point->created_at = $data['created_at']; + $point->updated_at = $data['updated_at']; + $point->deleted_at = $data['deleted_at']; return $point; } diff --git a/app/src/app/Models/PruningType.php b/app/src/app/Models/PruningType.php index b53f9634..a2a6866e 100644 --- a/app/src/app/Models/PruningType.php +++ b/app/src/app/Models/PruningType.php @@ -23,6 +23,8 @@ protected static function mapDataToModel($data): PruningType $pruning_type->description = $data['description']; $pruning_type->photo_id = $data['photo_id']; $pruning_type->created_at = $data['created_at']; + $pruning_type->updated_at = $data['updated_at']; + $pruning_type->deleted_at = $data['deleted_at']; return $pruning_type; } diff --git a/app/src/app/Models/Role.php b/app/src/app/Models/Role.php index 016ece72..6841339b 100644 --- a/app/src/app/Models/Role.php +++ b/app/src/app/Models/Role.php @@ -17,6 +17,8 @@ protected static function mapDataToModel($data): Role $role->id = $data['id']; $role->name = $data['name']; $role->created_at = $data['created_at']; + $role->updated_at = $data['updated_at']; + $role->deleted_at = $data['deleted_at']; return $role; } diff --git a/app/src/app/Models/Route.php b/app/src/app/Models/Route.php index 386e74ee..b958350c 100644 --- a/app/src/app/Models/Route.php +++ b/app/src/app/Models/Route.php @@ -20,6 +20,8 @@ protected static function mapDataToModel($data): Route $route->distance = $data['distance']; $route->travel_time = $data['travel_time']; $route->created_at = $data['created_at']; + $route->updated_at = $data['updated_at']; + $route->deleted_at = $data['deleted_at']; return $route; } diff --git a/app/src/app/Models/Sensor.php b/app/src/app/Models/Sensor.php index 8929281d..dc322a50 100644 --- a/app/src/app/Models/Sensor.php +++ b/app/src/app/Models/Sensor.php @@ -21,7 +21,9 @@ protected static function mapDataToModel($data): Sensor $sensor->model = $data["model"]; $sensor->class = $data["class"]; $sensor->is_active = $data["is_active"]; - $sensor->created_at = $data["created_at"]; + $sensor->created_at = $data['created_at']; + $sensor->updated_at = $data['updated_at']; + $sensor->deleted_at = $data['deleted_at']; return $sensor; } diff --git a/app/src/app/Models/SensorHistory.php b/app/src/app/Models/SensorHistory.php index 0b1e1d47..03ca55e2 100644 --- a/app/src/app/Models/SensorHistory.php +++ b/app/src/app/Models/SensorHistory.php @@ -22,6 +22,8 @@ protected static function mapDataToModel($data): SensorHistory $sensor_history->humidity = $data['humidity']; $sensor_history->inclination = $data['inclination']; $sensor_history->created_at = $data['created_at']; + $sensor_history->updated_at = $data['updated_at']; + $sensor_history->deleted_at = $data['deleted_at']; return $sensor_history; } diff --git a/app/src/app/Models/Task.php b/app/src/app/Models/Task.php index d3bf9f5a..d458d80b 100644 --- a/app/src/app/Models/Task.php +++ b/app/src/app/Models/Task.php @@ -26,6 +26,8 @@ protected static function mapDataToModel($data): Task $task->notes = $data['notes']; $task->route_id = $data['route_id']; $task->created_at = $data['created_at']; + $task->updated_at = $data['updated_at']; + $task->deleted_at = $data['deleted_at']; return $task; } diff --git a/app/src/app/Models/TaskType.php b/app/src/app/Models/TaskType.php index 3b3d172d..b4b15890 100644 --- a/app/src/app/Models/TaskType.php +++ b/app/src/app/Models/TaskType.php @@ -15,13 +15,15 @@ protected static function getTableName(): string protected static function mapDataToModel($data): TaskType { - $task = new self(); - $task->id = $data['id']; - $task->name = $data['name']; - $task->photo_id = $data['photo_id']; - $task->created_at = $data['created_at']; + $task_type = new self(); + $task_type->id = $data['id']; + $task_type->name = $data['name']; + $task_type->photo_id = $data['photo_id']; + $task_type->created_at = $data['created_at']; + $task_type->updated_at = $data['updated_at']; + $task_type->deleted_at = $data['deleted_at']; - return $task; + return $task_type; } public function photo(): ?Photo diff --git a/app/src/app/Models/TreeType.php b/app/src/app/Models/TreeType.php index da153408..c27b0140 100644 --- a/app/src/app/Models/TreeType.php +++ b/app/src/app/Models/TreeType.php @@ -22,6 +22,9 @@ protected static function mapDataToModel($data): TreeType $tree_type->family = $data['family']; $tree_type->genus = $data['genus']; $tree_type->species = $data['species']; + $tree_type->created_at = $data['created_at']; + $tree_type->updated_at = $data['updated_at']; + $tree_type->deleted_at = $data['deleted_at']; return $tree_type; } diff --git a/app/src/app/Models/User.php b/app/src/app/Models/User.php index 5f36594b..f6b11e27 100644 --- a/app/src/app/Models/User.php +++ b/app/src/app/Models/User.php @@ -43,6 +43,8 @@ protected static function mapDataToModel($data): User $user->role = $data['role']; $user->photo_id = $data['photo_id']; $user->created_at = $data['created_at']; + $user->updated_at = $data['updated_at']; + $user->deleted_at = $data['deleted_at']; return $user; } diff --git a/app/src/app/Models/WorkOrder.php b/app/src/app/Models/WorkOrder.php index abf14a12..ef86b4bc 100644 --- a/app/src/app/Models/WorkOrder.php +++ b/app/src/app/Models/WorkOrder.php @@ -13,12 +13,14 @@ protected static function getTableName(): string protected static function mapDataToModel($data): WorkOrder { - $order = new self(); - $order->id = $data['id']; - $order->contract_id = $data['contract_id']; - $order->created_at = $data['created_at']; - - return $order; + $work_order = new self(); + $work_order->id = $data['id']; + $work_order->contract_id = $data['contract_id']; + $work_order->created_at = $data['created_at']; + $work_order->updated_at = $data['updated_at']; + $work_order->deleted_at = $data['deleted_at']; + + return $work_order; } public function report(): WorkReport diff --git a/app/src/app/Models/WorkReport.php b/app/src/app/Models/WorkReport.php index 9e105ed4..5f349ae7 100644 --- a/app/src/app/Models/WorkReport.php +++ b/app/src/app/Models/WorkReport.php @@ -17,14 +17,16 @@ protected static function getTableName(): string protected static function mapDataToModel($data): WorkReport { - $workReport = new self(); - $workReport->id = $data['id']; - $workReport->work_order_id = $data['work_order_id']; - $workReport->observation = $data['observation']; - $workReport->spent_fuel = $data['spent_fuel']; - $workReport->created_at = $data['created_at']; - - return $workReport; + $work_report = new self(); + $work_report->id = $data['id']; + $work_report->work_order_id = $data['work_order_id']; + $work_report->observation = $data['observation']; + $work_report->spent_fuel = $data['spent_fuel']; + $work_report->created_at = $data['created_at']; + $work_report->updated_at = $data['updated_at']; + $work_report->deleted_at = $data['deleted_at']; + + return $work_report; } public function workOrder(): WorkOrder diff --git a/app/src/app/Models/Zone.php b/app/src/app/Models/Zone.php index f528b110..53397b41 100644 --- a/app/src/app/Models/Zone.php +++ b/app/src/app/Models/Zone.php @@ -21,6 +21,8 @@ protected static function mapDataToModel($data): Zone $zone->id = $data['id']; $zone->point_id = $data['point_id']; $zone->created_at = $data['created_at']; + $zone->updated_at = $data['updated_at']; + $zone->deleted_at = $data['deleted_at']; return $zone; } @@ -75,6 +77,8 @@ public static function getPredefinedZonesWithElements(): array $zone->predefined->name = $row['predefined_name']; $zone->predefined->photo_id = $row['predefined_photo_id']; $zone->predefined->created_at = $row['created_at']; + $zone->predefined->updated_at = $row['updated_at']; + $zone->predefined->deleted_at = $row['deleted_at']; $groupedZones[$row['id']] = $zone; } diff --git a/app/src/app/Models/ZonePredefined.php b/app/src/app/Models/ZonePredefined.php index 3ae4b5e5..e67bd4a9 100644 --- a/app/src/app/Models/ZonePredefined.php +++ b/app/src/app/Models/ZonePredefined.php @@ -17,14 +17,16 @@ protected static function getTableName(): string protected static function mapDataToModel($data): ZonePredefined { - $predefined = new self(); - $predefined->id = $data['id']; - $predefined->zone_id = $data['zone_id']; - $predefined->name = $data['name']; - $predefined->photo_id = $data['photo_id']; - $predefined->created_at = $data['created_at']; - - return $predefined; + $zone_predefined = new self(); + $zone_predefined->id = $data['id']; + $zone_predefined->zone_id = $data['zone_id']; + $zone_predefined->name = $data['name']; + $zone_predefined->photo_id = $data['photo_id']; + $zone_predefined->created_at = $data['created_at']; + $zone_predefined->updated_at = $data['updated_at']; + $zone_predefined->deleted_at = $data['deleted_at']; + + return $zone_predefined; } public function photo(): ?Photo