Files
thetool/application/WarehouseHistory/WarehouseHistoryModel.php
2024-07-16 06:55:46 +00:00

66 lines
2.0 KiB
PHP

<?php
//TODO: maybe convert this to use with TTCrudBaseModel
class WarehouseHistoryModel {
public int $id;
public string $table;
public int $row_id;
public string $key;
public string $old_value;
public string $new_value;
public string $note;
public int $user_id;
public string $user_name;
public int $create;
public function __construct($data = []) {
foreach ($data as $field => $value) {
if (property_exists(get_called_class(), $field)) {
$this->$field = $value;
}
}
}
public static function create($data) {
$FronkDB = FronkDB::singleton();
$db = $FronkDB->link;
$sql = /** @lang text */ "INSERT INTO `WarehouseHistory` (`table`, `row_id`, `key`, `old_value`, `new_value`, `note`, `user_id`, `create`) VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = $db->prepare($sql);
$stmt->execute([
$data["table"],
$data["row_id"],
$data["key"],
$data["old_value"],
$data["new_value"],
$data["note"],
$data["user_id"],
$data["create"]
]);
return $stmt->insert_id;
}
/**
* Retrieves an array of WarehouseHistoryModel objects by row ID.
*
* @param int $id The row ID.
* @param string $table The table name.
* @return WarehouseHistoryModel[] Array of WarehouseHistoryModel objects.
*/
public static function getByRowId(int $id, string $table): array {
$db = FronkDB::singleton();
$id = $db->escape($id);
$sql = /** @lang text */ "SELECT WH.*, W.name as user_name FROM `WarehouseHistory` WH
LEFT JOIN `Worker` W ON WH.user_id = W.id
WHERE `row_id` = $id AND `table` = '$table' ORDER BY `create` DESC";
$result = $db->query($sql);
$rows = [];
while ($row = $result->fetch_assoc()) {
$rows[] = new WarehouseHistoryModel($row);
}
return $rows;
}
}