Skip to content

Commit eea6ac5

Browse files
committed
update
1 parent 344ff1b commit eea6ac5

1 file changed

Lines changed: 35 additions & 7 deletions

File tree

src/Orm.php

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@
55
class Orm
66
{
77
/**
8-
* @param array<string, ?string> $object
8+
* Inserts a new record into the specified table.
9+
*
10+
* @param string $tableName The name of the table to insert into.
11+
* @param array<string, ?string> $object An associative array representing the record to insert, where keys are column names and values are the corresponding values.
12+
* @return int The ID of the newly inserted record. If no fields are provided, returns 0.
913
*/
1014
public static function insert(string $tableName, array $object): int
1115
{
@@ -27,9 +31,15 @@ public static function insert(string $tableName, array $object): int
2731
}
2832

2933
/**
30-
* @param array<string, ?string> $object
34+
* Updates an existing record in the specified table by its ID.
35+
*
36+
* @param string $tableName The name of the table to update.
37+
* @param array<string, ?string> $object An associative array representing the record to insert, where keys are column names and values are the corresponding values.
38+
* @param string|int $id The ID of the record to update.
39+
* @param string $idField The name of the ID field (default is 'id').
40+
* @return bool Returns true if the update was successful, false otherwise.
3141
*/
32-
public static function update(string $tableName, array $object, string|int $id, string $idField = 'id'): int
42+
public static function update(string $tableName, array $object, string|int $id, string $idField = 'id'): bool
3343
{
3444
if (isset($object[$idField])) {
3545
unset($object[$idField]);
@@ -51,16 +61,34 @@ public static function update(string $tableName, array $object, string|int $id,
5161
$qmarks = implode(',', $qmarks);
5262
$sql = "UPDATE `$tableName` SET `$fields` = ? WHERE `$idField` = ?";
5363
$params[] = $id;
54-
var_dump([$sql, $params]);
55-
return DB::update($sql, $params);
64+
return DB::update($sql, $params) ? true : false;
5665
}
5766

5867
/**
59-
* @return array<string, ?string> $object
68+
* Selects a record from the specified table by its ID.
69+
*
70+
* @param string $tableName The name of the table to select from.
71+
* @param string|int $id The ID of the record to select.
72+
* @param string $idField The name of the ID field (default is 'id').
73+
* @return array<string, ?string> $object An associative array representing the selected record, where keys are column names and values are the corresponding values. If no record is found, an empty array is returned.
6074
*/
6175
public static function select(string $tableName, string|int $id, string $idField = 'id'): array
6276
{
6377
$sql = "SELECT * FROM `$tableName` WHERE `$idField` = ?";
64-
return DB::selectOne($sql, [$id])[$tableName] ?? [];
78+
return DB::selectOne($sql, $id)[$tableName] ?? [];
79+
}
80+
81+
/**
82+
* Deletes a record from the specified table by its ID.
83+
*
84+
* @param string $tableName The name of the table.
85+
* @param string|int $id The ID of the record to delete.
86+
* @param string $idField The name of the ID field (default is 'id').
87+
* @return bool Returns true if the deletion was successful, false otherwise.
88+
*/
89+
public static function delete(string $tableName, string|int $id, string $idField = 'id'): bool
90+
{
91+
$sql = "DELETE FROM `$tableName` WHERE `$idField` = ?";
92+
return DB::delete($sql, $id) ? true : false;
6593
}
6694
}

0 commit comments

Comments
 (0)