Files
thetool/application/WarehouseHistory/WarehouseHistoryModel.php

72 lines
2.2 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;
$dataArr = [
$data["table"],
$data["row_id"],
$data["key"],
$data["old_value"],
$data["new_value"],
$data["note"],
$data["user_id"],
$data["create"]
];
$sqlValueStr = "(" . implode(", ", array_map(function ($item) use ($db) {
return "'" . $db->real_escape_string($item) . "'";
}, $dataArr)) . ")";
$sql = /** @lang text */ "INSERT INTO `WarehouseHistory` (`table`, `row_id`, `key`, `old_value`, `new_value`, `note`, `user_id`, `create`) VALUES $sqlValueStr";
$db->query($sql) or die($db->error);
return $db->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;
}
}